1、(8)运行eclipse,查看运行结果。2. 核心代码2.11. 数据库连接package org.lxh.info.dbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DatabaseConnection public static void main(String args Connection conn = null;Statemen
2、t stmt = null;ResultSet rs = null;try Class.forName(oracle.jdbc.driver.OracleDriver;String url = jdbc:oracle:thin:192.168.0.128:1521/WFJconn = DriverManager.getConnection(url, hr, stmt = conn.createStatement(;String sql = select * from departmentsrs = stmt.executeQuery(sql;while (rs.next( System.out
3、.print(rs.getInt(department_idSystem.out.print(tSystem.out.print(rs.getString(department_namemanager_idlocation_idSystem.out.println(; catch (ClassNotFoundException e e.printStackTrace(; catch (SQLException e finally if (rs != null rs.close(;if (stmt !stmt.close(;if (conn !conn.close(;/ TODO Auto-ge
4、nerated catch block2. 生成get set方法package edu;public class Country private String department_id;private String department_name;private String area;private String population;public String getdepartment_id( return department_id;public void setdepartment_id(String department_name this.name = department_
5、id;public String getdepartment_name( return department_name;public void setdepartment_name(String department_name this.department_name = department_name;public String getmanager_id( return manager_id;public void setmanager_id(String manager_id this.manager_id= manager_id;public String getlocation_id
6、( return location_id;public void setlocation_id(String location_idn this.location_id = location_id;3实现方法package tuxinghua;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.Serializable;import java.util.ArrayList;import java.util.List;import j
7、avax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.table.DefaultTableModel;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class AppStudent extends
8、 JFrameprivate JTextField department_idField;private JTextField department_nameField;private JTextField manager_idField;private JTextField location_idField;private JTable table;private DefaultTableModel model;private String columns = ;private List data;private Student tmp; public AppStudent( data =
9、new ArrayList(;getContentPane(.setLayout(null;JLabel lblemployee = new JLabel(lblemployee.setBounds(12, 10, 220, 13;getContentPane(.add(lblemployee;department_idField = new JTextField(;department_idField.setBounds(100, 7, 96, 19;getContentPane(.add(department_idField;department_idField.setColumns(10
10、;JLabel lblAge = new JLabel(lblAge.setBounds(252, 10, 220, 13;getContentPane(.add(lblAge;department_nameField = new JTextField(;department_nameField.setBounds(365, 7, 96, 19;getContentPane(.add(department_nameField;department_nameField.setColumns(10;JLabel lblStuno = new JLabel(lblStuno.setBounds(12
11、, 36, 220, 13;getContentPane(.add(lblStuno;manager_idField = new JTextField(;manager_idField.setColumns(10;manager_idField.setBounds(100, 33, 96, 19;getContentPane(.add(manager_idField;JLabel lblClass = new JLabel(lblClass.setBounds(252, 36, 220, 13;getContentPane(.add(lblClass;location_idField = ne
12、w JTextField(;location_idField.setColumns(10;location_idField.setBounds(365, 33, 96, 19;getContentPane(.add(location_idField;JButton btnAdd = new JButton(增加btnAdd.addActionListener(new ActionListener( public void actionPerformed(ActionEvent e add(;btnAdd.setBounds(75, 59, 77, 21;getContentPane(.add(
13、btnAdd;JButton btnDel = new JButton(删除btnDel.addActionListener(new ActionListener( del(;btnDel.setBounds(180, 59, 77, 21;getContentPane(.add(btnDel;JButton btnUpdate = new JButton(更新btnUpdate.addActionListener(new ActionListener( update(;btnUpdate.setBounds(280, 59, 77, 21;getContentPane(.add(btnUpd
14、ate;JButton btnFind = new JButton(查找btnFind.addActionListener(new ActionListener( find(;btnFind.setBounds(380, 59, 77, 21;getContentPane(.add(btnFind;model = new DefaultTableModel(columns, 0;table = new JTable(model;table.addMouseListener(new MouseAdapter(public void mouseClicked(MouseEvent eint row
15、 = 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(;JScrollPane scrollPane = new J
16、ScrollPane(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;private Student getInput( Student stu = new Student(;stu.department_id= dep
17、artment_idField.getText(;stu.department_name = department_nameField.getText(;stu.manager_id = manager_idField.getText(;stu.location_id = location_idField.getText(;return stu;private void add( data.add(getInput(;showTable(;private void del( for (int i = 0; i 0 model.removeRow(0;new AppStudent(;class
18、Studentimplements Serializable public String department_id;public String department_name;public String manager_id;public String location_id;public boolean equals(Object obj return equals(Student obj;public boolean equals(Student obj boolean isdepartment_id = true;if (obj.department_id != null & !.eq
19、uals(obj.department_id isdepartment_id = department_id.equals(obj.department_id;boolean isdepartment_name = true;if (obj.department_name !.equals(obj.department_name isdepartment_name = department_name.equals(obj.department_name;boolean ismanager_id = true;if (obj.manager_id !.equals(obj.manager_id
20、ismanager_id = manager_id.equals(obj.manager_id;boolean islocation_id = true;if (obj.location_id !.equals(obj.location_id islocation_id = location_id.equals(obj.location_id;return isdepartment_id& isdepartment_name & ismanager_id & islocation_id;public String toArray( return new String department_id
21、, department_name, manager_id, location_idpublic void setVisible(boolean b / TODO Auto-generated method stub3. 实训总结通过这次实训我发现了自己许多的缺点和不足,我一定会好好找出不足,尽最大可能去改正,不断进步发展。对于本次实训,我表示很遗憾,未能达到预期的效果,归根到底还是关于java的知识没学好。我认为基础知识是很重要的,图形化界面要在eclipse中实现必须要熟悉jframe的功能,只有熟知jframe的所有功能才能对图形化界面做出来,但是我对之一部分的了解是少之又少,不足以通过jframe来实现图形化界面,并且在里面连接数据库,实现oracle中hr用户中department 和employees这两张表的增删改查。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1