当前位置:文档之家› JAVA swing界面实现数据库增删改查

JAVA swing界面实现数据库增删改查

数据库程序设计大作业班级:2012级软件外包服务一班学号:201215140117姓名:。

时间:2013-6-191.功能描述1.1 功能简介用swing做出图形化界面形式,实现数据库的增删改查把员工表跟部门表连接起来,实现数据的增加,删除,修改,查找。

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

(2)在网络中心修改pc机上的VMware Network Adapter的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.11.数据库连接package .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;Statement stmt = null;ResultSet rs = null;try {Class.forName("oracle.jdbc.driver.OracleDriver");String url = "jdbc:oracle:thin:@192.168.0.128:1521/WFJ";conn = DriverManager.getConnection(url, "hr", "hr");stmt = conn.createStatement();String sql = "select * from departments";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 (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} finally {try {if (rs != null) {rs.close();}if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}2.生成get set方法package edu;public class Country {private String department_id;private String department_name;private String area;private String population;public String get department_id() {return department_id;}public void set department_id(String department_name) { = department_id;}public String get department_name() {return department_name;}public void set department_name(String department_name) { this.department_name = department_name;}public String get manager_id() {return manager_id;}public void set manager_id(String manager_id) {this.manager_id= manager_id;}public String get location_id() {return location_id;}public void set location_id(String location_id n) {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.sql.DriverManager;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import javax.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 JFrame{private JTextField department_idField;private JTextField department_nameField;private JTextField manager_idField;private JTextField location_idField;private JTable table;private DefaultTableModel model;private String[] columns = { "department_id", "department_name", "manager_id", "location_id"};private List data;private Student tmp;public AppStudent() {data = new ArrayList();getContentPane().setLayout(null);JLabel lblemployee = new JLabel("department_id");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);JLabel lblAge = new JLabel("department_name");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("manager_id"); lblStuno.setBounds(12, 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("location_id"); lblClass.setBounds(252, 36, 220, 13); getContentPane().add(lblClass);location_idField = new 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(btnAdd);JButton btnDel = new JButton("删除");btnDel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { del();}});btnDel.setBounds(180, 59, 77, 21); getContentPane().add(btnDel);JButton btnUpdate = new JButton("更新"); btnUpdate.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {update();}});btnUpdate.setBounds(280, 59, 77, 21);getContentPane().add(btnUpdate);JButton btnFind = new JButton("查找");btnFind.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {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 e){int row = 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 JScrollPane(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= department_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 < data.size(); i++){if (tmp.equals(data.get(i))){data.remove(i);break;}}showTable();}private void update() {Student stu = getInput();for (int i = 0; i < data.size(); i++){if (tmp.equals(data.get(i))) {data.remove(i);data.add(i, stu);break;}}showTable();}private void find() {removeRows();Student stu = getInput();for (int i = 0; i < data.size(); i++) {Student tmp = (Student) data.get(i);if (tmp.equals(stu)) {model.addRow(tmp.toArray());break;}}}private void showTable() {removeRows();for (int i = 0; i < data.size(); i++){Student stu = (Student) data.get(i);model.addRow(stu.toArray());}}private void removeRows() {while (model.getRowCount() > 0) {model.removeRow(0);}}public static void main(String[] args) {new AppStudent();}}class 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 && !"".equals(obj.department_id)) {isdepartment_id = department_id.equals(obj.department_id);}boolean isdepartment_name = true;if (obj.department_name != null && !"".equals(obj.department_name)) { isdepartment_name = department_name.equals(obj.department_name);}boolean ismanager_id = true;if (obj.manager_id != null && !"".equals(obj.manager_id)) {ismanager_id = manager_id.equals(obj.manager_id);}boolean islocation_id = true;if (obj.location_id != null && !"".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, department_name, manager_id, location_id};}public void setVisible(boolean b) {// TODO Auto-generated method stub}}3. 实训总结通过这次实训我发现了自己许多的缺点和不足,我一定会好好找出不足,尽最大可能去改正,不断进步发展。

相关主题