1、/ 项目目旳:建立员工管理系统第一种版本。/ 实现图形顾客界面旳员工信息查询、增、删、改;与数据库建立,并将修改成果时时保留到数据库。/ 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库)import java.sql.*;import java.awt.*;import .*;import javax.swing.*;public class EmpManageSys implements ActionListener Connection con;Statement stmt;String sql;ResultSet rs;StringBuffer sb =
2、new StringBuffer();JTextField jtf2 = new JTextField(张飞,10);JTextField jtf3 = new JTextField(10);JTextField jtf4 = new JTextField(10);JTextField jtf7 = new JTextField(10);JTextField jtf5 = new JTextField(10);JTextField jtf6 = new JTextField(10);JTextField jtf8 = new JTextField(10);JTextField jtf9 = n
3、ew JTextField(10);JTextArea jta10 = new JTextArea(50, 40);public void actionPerformed(ActionEvent e) String str = e.getActionCommand();if (查询.equals(str) /if check the button of 查询,then go to method of searchEmp()searchEmp(); else if (增长.equals(str) /if check the button of 增长,then go to method of ad
4、dEmp()addEmp(); else if (修改.equals(str) /if check the button of 修改,then go to method of alterEmp()alterEmp(); else if (撤除.equals(str) /if check the button of 撤除,then go to method of deleteEmp()deleteEmp();/this is the constructor.EmpManageSys() createGUI();connectToDataBase();/ searchEmp();/ addEmp(
5、);/ deleteEmp();alterEmp();/connect to the database by using the method getConnection from the class of JdbcUtil./You can find the class of JdbcUtil at the button of this page.public Connection connectToDataBase() con = JdbcUtil.getConnection();(con);return con;public void searchEmp() jta10.setText(
6、);try stmt = con.createStatement();/ sql = select * from Mstar where id= + jtf3.getText();/ sql1=select * from sd100343 where =+jtf3.getText();sql = select * from Mstar where chineseName=+ jtf2.getText().toLowerCase().trim() + or id=+ jtf3.getText().toLowerCase().trim() + or engName=+ jtf4.getText()
7、.toLowerCase().trim() + or UNIT=+ jtf5.getText().toLowerCase().trim() + or TEAM=+ jtf6.getText().toLowerCase().trim() + or Phone=+ jtf7.getText().toLowerCase().trim() + or region=+ jtf8.getText().toLowerCase().trim() + or busStation=+ jtf9.getText().toLowerCase().trim();stmt.executeQuery(sql);rs = s
8、tmt.getResultSet();ResultSetMetaData meta = rs.getMetaData();int cols = meta.getColumnCount();while (rs.next() for (int i = 1; i = cols; i+) sb.append( + meta.getColumnName(i) + =);sb.append(rs.getString(i);sb.append(n);jta10.setText(sb.toString(); catch (SQLException e11) e11.printStackTrace();publ
9、ic void addEmp() try stmt = con.createStatement();sql = update Mstar values( + jtf2.getText() + jtf3.getText()+ jtf4.getText() + jtf5.getText() + jtf6.getText()+ jtf7.getText() + jtf8.getText() + jtf9.getText() + );int i = stmt.getUpdateCount();if (jtf2.getText() != null) & (jtf4.getText() != null)&
10、 (jtf6.getText() != null) & (jtf7.getText() != null) stmt.executeUpdate(sql);jta10.setText(添加记录成功 + i + 条); else jta10.setText(带*号项为添加记录时不能为空); catch (SQLException e1) e1.printStackTrace();public void deleteEmp() searchEmp();try stmt = con.createStatement();sql = delete from Mstar where chineseName=
11、+ jtf2.getText().toLowerCase().trim() + or id=+ jtf3.getText().toLowerCase().trim() + or engName=+ jtf4.getText().toLowerCase().trim() + or UNIT=+ jtf5.getText().toLowerCase().trim() + or TEAM=+ jtf6.getText().toLowerCase().trim() + or Phone=+ jtf7.getText().toLowerCase().trim() + or region=+ jtf8.g
12、etText().toLowerCase().trim() + or busStation=+ jtf9.getText().toLowerCase().trim();stmt.executeUpdate(sql);int i = stmt.getUpdateCount();jta10.setText(撤除操作成功 + i + 条); catch (SQLException e) e.printStackTrace();public void alterEmp() searchEmp();sql = update Mstar set chineseName=+ jtf2.getText().t
13、oLowerCase().trim() + and id=+ jtf3.getText().toLowerCase().trim() + and engName=+ jtf4.getText().toLowerCase().trim() + and UNIT=+ jtf5.getText().toLowerCase().trim() + and TEAM=+ jtf6.getText().toLowerCase().trim() + and Phone=+ jtf7.getText().toLowerCase().trim() + and region=+ jtf8.getText().toL
14、owerCase().trim() + and busStation=+ jtf9.getText().toLowerCase().trim();int i = 0;try stmt.executeUpdate(sql);i = stmt.getUpdateCount(); catch (SQLException e) e.printStackTrace();jta10.setText(修改操作成功 + i + 条);public void createGUI() JFrame jf = new JFrame(员工信息管理系统);jf.setLayout(new GridLayout(2, 1
15、);/ jf.setLayout(new GridLayout(10,2);JPanel jp00 = new JPanel(new GridLayout(5, 4);JPanel jp1 = new JPanel();JButton jb11 = new JButton(查询);jb11.addActionListener(this);JButton jb12 = new JButton(增长);jb12.addActionListener(this);jp1.add(jb11);jp1.add(jb12);jp00.add(jp1);JPanel jp11 = new JPanel();J
16、Button jb111 = new JButton(修改);jb111.addActionListener(this);JButton jb112 = new JButton(撤除);jb112.addActionListener(this);jp11.add(jb111);jp11.add(jb112);jp00.add(jp11);JPanel jp2 = new JPanel();JLabel jl2 = new JLabel(中 文 名 * );jp2.add(jl2);jp2.add(jtf2);jp00.add(jp2);JPanel jp3 = new JPanel();JLa
17、bel jl3 = new JLabel(工 号 );jp3.add(jl3);jp3.add(jtf3);jp00.add(jp3);JPanel jp4 = new JPanel();JLabel jl4 = new JLabel(EngName*);jp4.add(jl4);jp4.add(jtf4);jp00.add(jp4);JPanel jp5 = new JPanel();JLabel jl5 = new JLabel(UNIT );jp5.add(jl5);jp5.add(jtf5);jp00.add(jp5);JPanel jp6 = new JPanel();JLabel
18、jl6 = new JLabel(TEAM* );jp6.add(jl6);jp6.add(jtf6);jp00.add(jp6);JPanel jp7 = new JPanel();JLabel jl7 = new JLabel(Phone* );jp7.add(jl7);jp7.add(jtf7);jp00.add(jp7);JPanel jp8 = new JPanel();JLabel jl8 = new JLabel(区域 );jp8.add(jl8);jp8.add(jtf8);jp00.add(jp8);JPanel jp9 = new JPanel();JLabel jl9 =
19、 new JLabel(公交站 );jp9.add(jl9);jp9.add(jtf9);jp00.add(jp9);jf.add(jp00);JPanel jp01 = new JPanel();jta10.setText(-顾客使用手册- n1、查询:选择一种字段,如 EngName 在文本框中输入对应内容,点击查询n2、增长:在各个文本框中输入对应内容后,点击增长。n3、修改:先查询到你要旳记录,在更改对应内容,点击修改。n4、撤除:类似于修改操作);jp01.add(jta10);jf.add(jp01);jf.setSize(700, 500);jf.setDefaultCloseO
20、peration(JFrame.EXIT_ON_CLOSE);jf.setVisible(true);public static void main(String args) new EmpManageSys();/-SQL工具类JdbcUtil:用于实现数据库连接和数据库关闭。上面程序用到了这个类import java.sql.*;public class JdbcUtil/load driver from file statictryString driverName =;Class.forName(driverName);catch(Exception e)e.printStackTra
21、ce();/establish connectionpublic static Connection getConnection()Connection con = null;tryString url = jdbc:oracle:thin:192.168.0.26:1521:tarena;String usr = openlab;String pwd = open123;con = DriverManager.getConnection(url,usr,pwd);catch(Exception e)e.printStackTrace();return con;/close resultSet
22、,statement and connectionpublic static void close(ResultSet rs,Statement stmt,Connection con)tryif(rs!=null) rs.close();catch(Exception ex)ex.printStackTrace();tryif(stmt!=null) stmt.close();catch(Exception ex)ex.printStackTrace();tryif(con!=null) con.close();catch(Exception ex)ex.printStackTrace();