《簡單的JAVA員工信息管理系統(tǒng)源碼》由會員分享,可在線閱讀,更多相關《簡單的JAVA員工信息管理系統(tǒng)源碼(7頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、// 項目目標:建立員工管理系統(tǒng)第一個版本。
// 實現(xiàn)圖形用戶界面的員工信息查詢、增、刪、改;與數(shù)據(jù)庫建立,并將修改
結果時時保存到數(shù)據(jù)庫。
// 模塊: 1.GUI 界面 2. 數(shù)據(jù)庫連接 3. 查詢 4. 增、 5 刪 6 改(操作數(shù)據(jù)庫)
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EmpManageSys implements ActionListener {
Connection con;
Statement s
2、tmt;
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 JTe
3、xtField(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 searchE
4、mp() 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 g
5、o 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
6、 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.get
7、Text();
// 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
8、().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 m
9、eta = 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();
10、
}
}
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() !=
11、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
12、();
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="
+
13、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 + " 條");
14、} 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.
15、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
16、 = stmt.getUpdateCount();
} catch (SQLException e) { e.printStackTrace();
}
jta10.setText(" 修改操作成功 " + i + " 條");
}
public void createGUI() {
JFrame jf = new JFrame(" 員工信息管理系統(tǒng) ");
jf.setLayout(new GridLayout(2, 1));
// jf.setLayout(new GridLayout(10,2));
JPanel jp00 = new JPanel(new GridLay
17、out(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.ad
18、dActionListener(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
19、= 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
20、 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(" 區(qū)域 ");
jp8.add(jl8);
jp8.ad
21、d(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 在
文本框中輸入相應內(nèi)容, 點擊查詢 \n2 、 增加: 在各個文本框中輸入相應內(nèi)容后,
點擊增加。 \n3 、 修改: 先查詢到
22、你要的記錄, 在更改相應內(nèi)容, 點擊修改。 \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();
}
}
//
SQLT具類JdbcUtil :用于實現(xiàn)數(shù)據(jù)庫連接和數(shù)據(jù)庫關閉。上面程序用到了這個 類
imp
23、ort 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 = n
24、ull;
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 clos
25、e(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(); }
}
}