标题:员工信息管理系统(源码)
只看楼主
BaoZhongke
Rank: 2
等 级:论坛游民
帖 子:8
专家分:20
注 册:2020-11-8
 问题点数:0 回复次数:2 
员工信息管理系统(源码)
// 项目目标:建立员工管理系统第一个版本。
// 实现图形用户界面的员工信息查询、增、删、改;与数据库建立,并将修改结果时时保存到数据库。
// 模块:1.GUI界面 2.数据库连接 3.查询 4.增、5删 6改(操作数据库)

import java.sql.*;
 import java.awt.*;
 import java.awt.event.*;

import javax.swing.*;

public class EmpManageSys implements ActionListener {
 Connection con;
 Statement stmt;
 String sql;
 ResultSet rs;
 StringBuffer sb = 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 = new 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 addEmp()
 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();
 // 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();
 System.out.println(con);
 return con;

}

public void searchEmp() {
 jta10.setText("");
 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().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 = stmt.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();
 }

}

public 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)
 && (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="
 + 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.getText().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().toLowerCase().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().toLowerCase().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));
 // 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();
 JButton 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();
 JLabel 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 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 = 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.setDefaultCloseOperation(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
 static{
 try{
 String driverName =
 "oracle.jdbc.driver.OracleDriver";
 Class.forName(driverName);
 }catch(Exception e){
 e.printStackTrace();
 }
 }
 //establish connection
 public static Connection getConnection(){
 Connection con = null;
 try{
 String 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,statement and connection
 public static void close(ResultSet rs,Statement stmt,Connection con){
 try{
 if(rs!=null) rs.close();
 }catch(Exception ex){
 ex.printStackTrace();
 }
 try{
 if(stmt!=null) stmt.close();
 }catch(Exception ex){
 ex.printStackTrace();
 }
 try{
 if(con!=null) con.close();
 }catch(Exception ex){
 ex.printStackTrace();
 }
 }
 }
搜索更多相关主题的帖子: sql con new trim add 
2020-11-08 07:10
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:0 
程序代码:
package huawei.gui.test;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class EmpManageSys implements ActionListener {
    private List<String[]> infos = new ArrayList<>();
    private List<String> lblNames = Arrays.asList("中 文 名 * ","工 号","EngName","UNIT","TEAM*",
            "Phone*","区域 ","公交站");
    private JTextField[] txts = new JTextField[lblNames.size()];
    private JTextArea resultJta = new JTextArea();
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        if ("查询".equals(str)) {
            searchEmp();
        } else if ("增加".equals(str)) {
            addEmp();
        } else if ("修改".equals(str)) {
            alterEmp();
        } else if ("撤除".equals(str)) {
            deleteEmp();
        }
    }
    
    EmpManageSys() {
        createGUI();
        searchEmp();
    }
    
    public void searchEmp() {
        resultJta.setText("");
        for(String str:lblNames) {
            resultJta.append(str+"\t");
        }
        resultJta.append("\n");
        for(String[] strs:infos) {
            for(String str:strs) {
                resultJta.append(str+"\t");
            }
            resultJta.append("\n");
        }
    }
    
    public void addEmp() {
        String[] strs = new String[txts.length];
        for(int i=0;i<strs.length;i++) {
            strs[i] = txts[i].getText();
        }
        infos.add(strs);
        searchEmp();
    }
    
    public void deleteEmp() {
        searchEmp();
    }
    
    public void alterEmp() {
        searchEmp();
    }
    
    public void createGUI() {
        JFrame jframe = new JFrame("员工信息管理系统");
        jframe.setLayout(new GridLayout(2, 1));// 窗体2行一列
        JPanel jpUp = new JPanel(new GridLayout(5, 4));// 按钮,输入控件及左侧标题5行4列
        // 第一行,1-2列
        JPanel btnGroup1 = new JPanel();
        JButton btnSearch = new JButton("查询");
        btnSearch.addActionListener(this);
        JButton btnAdd = new JButton("增加");
        btnAdd.addActionListener(this);
        btnGroup1.add(btnSearch);
        btnGroup1.add(btnAdd);
        jpUp.add(btnGroup1);
        // 第一行,3-4列
        JPanel btnGroup2 = new JPanel();
        JButton btnUpdate = new JButton("修改");
        btnUpdate.addActionListener(this);
        JButton btnDel = new JButton("撤除");
        btnDel.addActionListener(this);
        btnGroup2.add(btnUpdate);
        btnGroup2.add(btnDel);
        jpUp.add(btnGroup2);
        // 第2-5行
        for(int i=0;i<txts.length;i++) {
            JPanel namePan = new JPanel();
            JLabel nameLbl = new JLabel(lblNames.get(i));
            namePan.add(nameLbl);
            txts[i] = new JTextField(10);
            namePan.add(txts[i]);
            jpUp.add(namePan);
        }
        // 添加到窗体
        jframe.add(jpUp);
        jframe.add(resultJta);
        jframe.setSize(700, 500);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);
    }
    
    public static void main(String[] args) {
        new EmpManageSys();
    }
}


剑栈风樯各苦辛,别时冰雪到时春
2020-11-10 09:54
tequilashou
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2020-7-29
得分:0 
兄弟 JAVA的图形界面没人用。图形界面用WEB  HTML来做的。还有不要用System.out.println() 输出 要用SLF4J 然后自己看了办,用JAVAlog LOG4 或 logBck.输出。最后数据库要用池技术 不要直接JDBC

[此贴子已经被作者于2020-12-1 16:04编辑过]

2020-12-01 16:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503815-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.147422 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved