JAVA swing界面实现数据库增删改查Word文档格式.docx
《JAVA swing界面实现数据库增删改查Word文档格式.docx》由会员分享,可在线阅读,更多相关《JAVA swing界面实现数据库增删改查Word文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
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"
"
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"
manager_id"
location_id"
System.out.println();
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
}finally{
try{
if(rs!
=null){
rs.close();
}
if(stmt!
stmt.close();
if(conn!
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.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={"
};
privateListdata;
privateStudenttmp;
publicAppStudent(){
data=newArrayList();
getContentPane().setLayout(null);
JLabellblemployee=newJLabel("
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("
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("
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("
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(){
del();
btnDel.setBounds(180,59,77,21);
getContentPane().add(btnDel);
JButtonbtnUpdate=newJButton("
更新"
btnUpdate.addActionListener(newActionListener(){
update();
btnUpdate.setBounds(280,59,77,21);
getContentPane().add(btnUpdate);
JButtonbtnFind=newJButton("
查找"
btnFind.addActionListener(newActionListener(){
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((String)table.getValueAt(row,0));
department_nameField.setText((String)table.getValueAt(row,2));
manager_idField.setText((String)table.getValueAt(row,3));
location_idField.setText((String)table.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<
data.size();
i++)
if(tmp.equals(data.get(i)))
{
data.remove(i);
break;
privatevoidupdate(){
Studentstu=getInput();
if(tmp.equals(data.get(i))){
data.remove(i);
data.add(i,stu);
break;
privatevoidfind(){
removeRows();
i++){
Studenttmp=(Student)data.get(i);
if(tmp.equals(stu)){
model.addRow(tmp.toArray());
privatevoidshowTable(){
Studentstu=(Student)data.get(i);
model.addRow(stu.toArray());
privatevoidremoveRows(){
while(model.getRowCount()>
0){
model.removeRow(0);
newAppStudent();
classStudent
implementsSerializable{
publicStringdepartment_id;
publicStringdepartment_name;
publicStringmanager_id;
publicStringlocation_id;
publicbooleanequals(Objectobj){
returnequals((Student)obj);
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!
.equals(obj.department_name)){
isdepartment_name=department_name.equals(obj.department_name);
booleanismanager_id=true;
if(obj.manager_id!
.equals(obj.manager_id)){
ismanager_id=manager_id.equals(obj.manager_id);
booleanislocation_id=true;
if(obj.location_id!
.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这两张表的增删改查。