JAVAswing界面实现数据库增删改查精.docx

上传人:b****6 文档编号:4322526 上传时间:2022-11-29 格式:DOCX 页数:12 大小:47.41KB
下载 相关 举报
JAVAswing界面实现数据库增删改查精.docx_第1页
第1页 / 共12页
JAVAswing界面实现数据库增删改查精.docx_第2页
第2页 / 共12页
JAVAswing界面实现数据库增删改查精.docx_第3页
第3页 / 共12页
JAVAswing界面实现数据库增删改查精.docx_第4页
第4页 / 共12页
JAVAswing界面实现数据库增删改查精.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

JAVAswing界面实现数据库增删改查精.docx

《JAVAswing界面实现数据库增删改查精.docx》由会员分享,可在线阅读,更多相关《JAVAswing界面实现数据库增删改查精.docx(12页珍藏版)》请在冰豆网上搜索。

JAVAswing界面实现数据库增删改查精.docx

JAVAswing界面实现数据库增删改查精

数据库程序设计

大作业

班级:

2012级软件外包服务一班

学号:

201215140117

姓名:

时间:

2013-6-19

1.功能描述

1.1功能简介

用swing做出图形化界面形式,实现数据库的增删改查

把员工表跟部门表连接起来,实现数据的增加,删除,修改,查找。

1.2实现步骤

(1)安装好虚拟机,并在其下确认oracle已安装成功(可以在dos下输入相关命令来检查是否安装成功)。

(2)在网络中心修改pc机上的VMwareNetworkAdapter的IP、子网页码(默认)、网关。

(3)修改虚拟机的IP、网掩码(默认)、网关,确保PC机上的网关和虚拟机的IP一致。

(在控制面板——>网络和共享中心——>本地连接3——>属性中,修改IP、网掩码)

(4)在PC机的dos命令控制台ping虚拟机的IP,确保正常,能ping通(即将虚拟机内外ping通)。

(5)配置好虚拟机下的oracle的数据库和监听。

(6)在eclipse中编写相关代码,并用jtable实现图形化界面,用以实现与数据库的连接和操作数据库等功能。

(7)在eclipse中导入数据库的驱动。

(8)运行eclipse,查看运行结果。

2.核心代码

2.1

1.数据库连接

packageorg.lxh.info.dbc;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

publicclassDatabaseConnection{

publicstaticvoidmain(String[]args{

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

Class.forName("oracle.jdbc.driver.OracleDriver";

Stringurl="jdbc:

oracle:

thin:

@192.168.0.128:

1521/WFJ";

conn=DriverManager.getConnection(url,"hr","hr";

stmt=conn.createStatement(;

Stringsql="select*fromdepartments";

rs=stmt.executeQuery(sql;

while(rs.next({

System.out.print(rs.getInt("department_id";

System.out.print("\t";

System.out.print(rs.getString("department_name";

System.out.print("\t";

System.out.print(rs.getInt("manager_id";

System.out.print("\t";

System.out.print(rs.getInt("location_id";

System.out.println(;

}

}catch(ClassNotFoundExceptione{

e.printStackTrace(;

}catch(SQLExceptione{

e.printStackTrace(;

}finally{

try{

if(rs!

=null{

rs.close(;

}

if(stmt!

=null{

stmt.close(;

}

if(conn!

=null{

conn.close(;

}

}catch(SQLExceptione{

//TODOAuto-generatedcatchblock

e.printStackTrace(;

}

}

}

}

2.生成getset方法

packageedu;

publicclassCountry{

privateStringdepartment_id;

privateStringdepartment_name;

privateStringarea;

privateStringpopulation;

publicStringgetdepartment_id({

returndepartment_id;

}

publicvoidsetdepartment_id(Stringdepartment_name{

this.name=department_id;

}

publicStringgetdepartment_name({

returndepartment_name;

}

publicvoidsetdepartment_name(Stringdepartment_name{

this.department_name=department_name;

}

publicStringgetmanager_id({

returnmanager_id;

}

publicvoidsetmanager_id(Stringmanager_id{

this.manager_id=manager_id;

}

publicStringgetlocation_id({

returnlocation_id;

}

publicvoidsetlocation_id(Stringlocation_idn{

this.location_id=location_id;

}

}

}

3实现方法

packagetuxinghua;

importjava.awt.Color;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.Serializable;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.util.ArrayList;

importjava.util.List;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JScrollPane;

importjavax.swing.JTable;

importjavax.swing.JTextField;

importjavax.swing.table.DefaultTableModel;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

publicclassAppStudentextendsJFrame{

privateJTextFielddepartment_idField;

privateJTextFielddepartment_nameField;

privateJTextFieldmanager_idField;

privateJTextFieldlocation_idField;

privateJTabletable;

privateDefaultTableModelmodel;

privateString[]columns={"department_id","department_name","manager_id","location_id"};

privateListdata;

privateStudenttmp;

publicAppStudent({

data=newArrayList(;

getContentPane(.setLayout(null;

JLabellblemployee=newJLabel("department_id";

lblemployee.setBounds(12,10,220,13;

getContentPane(.add(lblemployee;

department_idField=newJTextField(;

department_idField.setBounds(100,7,96,19;

getContentPane(.add(department_idField;

department_idField.setColumns(10;

JLabellblAge=newJLabel("department_name";

lblAge.setBounds(252,10,220,13;

getContentPane(.add(lblAge;

department_nameField=newJTextField(;

department_nameField.setBounds(365,7,96,19;

getContentPane(.add(department_nameField;

department_nameField.setColumns(10;

JLabellblStuno=newJLabel("manager_id";

lblStuno.setBounds(12,36,220,13;

getContentPane(.add(lblStuno;

manager_idField=newJTextField(;

manager_idField.setColumns(10;

manager_idField.setBounds(100,33,96,19;

getContentPane(.add(manager_idField;

JLabellblClass=newJLabel("location_id";

lblClass.setBounds(252,36,220,13;

getContentPane(.add(lblClass;

location_idField=newJTextField(;

location_idField.setColumns(10;

location_idField.setBounds(365,33,96,19;

getContentPane(.add(location_idField;

JButtonbtnAdd=newJButton("增加";

btnAdd.addActionListener(newActionListener({

publicvoidactionPerformed(ActionEvente{

add(;

}

}

;

btnAdd.setBounds(75,59,77,21;

getContentPane(.add(btnAdd;

JButtonbtnDel=newJButton("删除";

btnDel.addActionListener(newActionListener({

publicvoidactionPerformed(ActionEvente{

del(;

}

}

;

btnDel.setBounds(180,59,77,21;

getContentPane(.add(btnDel;

JButtonbtnUpdate=newJButton("更新";

btnUpdate.addActionListener(newActionListener({

publicvoidactionPerformed(ActionEvente{

update(;

}

}

;

btnUpdate.setBounds(280,59,77,21;

getContentPane(.add(btnUpdate;

JButtonbtnFind=newJButton("查找";

btnFind.addActionListener(newActionListener({

publicvoidactionPerformed(ActionEvente{

find(;

}

}

;

btnFind.setBounds(380,59,77,21;

getContentPane(.add(btnFind;

model=newDefaultTableModel(columns,0;

table=newJTable(model;

table.addMouseListener(newMouseAdapter(

{

publicvoidmouseClicked(MouseEvente

{

introw=table.getSelectedRow(;

department_idField.setText((Stringtable.getValueAt(row,0;

department_nameField.setText((Stringtable.getValueAt(row,2;

manager_idField.setText((Stringtable.getValueAt(row,3;

location_idField.setText((Stringtable.getValueAt(row,4;

tmp=getInput(;

}

};

JScrollPanescrollPane=newJScrollPane(table;

scrollPane.setBounds(12,100,571,248;

getContentPane(.add(scrollPane;

setDefaultCloseOperation(EXIT_ON_CLOSE;

setLocationRelativeTo(null;

setLocation(350,200;

setSize(601,380;

setResizable(false;

setVisible(true;

}

privateStudentgetInput({

Studentstu=newStudent(;

stu.department_id=department_idField.getText(;

stu.department_name=department_nameField.getText(;

stu.manager_id=manager_idField.getText(;

stu.location_id=location_idField.getText(;

returnstu;

}

privatevoidadd({

data.add(getInput(;

showTable(;

}

privatevoiddel({

for(inti=0;i

{

if(tmp.equals(data.get(i

{

data.remove(i;break;

}

}

showTable(;

}

privatevoidupdate({

Studentstu=getInput(;

for(inti=0;i

{

if(tmp.equals(data.get(i{

data.remove(i;

data.add(i,stu;

break;

}

}

showTable(;

}

privatevoidfind({

removeRows(;

Studentstu=getInput(;

for(inti=0;i

Studenttmp=(Studentdata.get(i;

if(tmp.equals(stu{

model.addRow(tmp.toArray(;

break;

}

}

}

privatevoidshowTable({

removeRows(;

for(inti=0;i

{

Studentstu=(Studentdata.get(i;

model.addRow(stu.toArray(;

}

}

privatevoidremoveRows({

while(model.getRowCount(>0{

model.removeRow(0;

}

}

publicstaticvoidmain(String[]args{

newAppStudent(;

}

}

classStudent

implementsSerializable{

publicStringdepartment_id;

publicStringdepartment_name;

publicStringmanager_id;

publicStringlocation_id;

publicbooleanequals(Objectobj{

returnequals((Studentobj;

}

publicbooleanequals(Studentobj{

booleanisdepartment_id=true;

if(obj.department_id!

=null&&!

"".equals(obj.department_id{

isdepartment_id=department_id.equals(obj.department_id;

}

booleanisdepartment_name=true;

if(obj.department_name!

=null&&!

"".equals(obj.department_name{

isdepartment_name=department_name.equals(obj.department_name;

}

booleanismanager_id=true;

if(obj.manager_id!

=null&&!

"".equals(obj.manager_id{

ismanager_id=manager_id.equals(obj.manager_id;

}

booleanislocation_id=true;

if(obj.location_id!

=null&&!

"".equals(obj.location_id{

islocation_id=location_id.equals(obj.location_id;

}

returnisdepartment_id&&isdepartment_name&&ismanager_id&&islocation_id;

}

publicString[]toArray({

returnnewString[]{department_id,department_name,manager_id,location_id

};

}

publicvoidsetVisible(booleanb{

//TODOAuto-generatedmethodstub

}

}

3.实训总结

 通过这次实训我发现了自己许多的缺点和不足,我一定会好好找出不足,尽最大可能去改正,不断进步发展。

对于本次实训,我表示很遗憾,未能达到预期的效果,归根到底还是关于java的知识没学好。

我认为基础知识是很重要的,图形化界面要在eclipse中实现必须要熟悉jframe的功能,只有熟知jframe的所有功能才能对图形化界面做出来,但是我对之一部分的了解是少之又少,不足以通过jframe来实现图形化界面,并且在里面连接数据库,实现oracle中hr用户中department和employees这两张表的增删改查。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 司法考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1