關鍵函式程式碼
(1)連線資料庫程式碼
ackage worker;//連線資料庫的語句
import java.sql.*;
publicclass DBConn {
Publicstatic String driverName
= "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//連線資料庫的驅動名
protectedstatic String dbURL
= "jdbc:sqlserver://localhost:1433;DatabaseName=職工考勤管理";//資料庫對映路徑
protectedstatic String dbuser = "sa"; //資料庫的登入名
protectedstatic String dbPwd = "123"; //資料庫的登入密碼
Connection conn; //申明一個數據庫連線
Statement stmt;//介面,可以寫SQL
PreparedStatement prepare;
public DBConn(String username,String userpassword){ //裝載時建立資料庫連結
try{
Class.forName(driverName);
Conn= DriverManager.getConnection(dbURL,username,userpassword);
System.out.println("成功");
}catch(Exception e){e.printStackTrace();}
}
}
(2)登陸介面程式碼
package worker;//系統登入介面的顯示
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
importjava.util.*;
importjava.util.Date;
import javax.swing.*;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.applet.*;
importjava.io.*;
classdbframeextends JFrame implements ActionListener{
DBConn c=new DBConn("sa","123");
//JLabel L2 = new JLabel(new ImageIcon("D:\\1.jpg"));
//JPanel p = new JPanel();
ImageIcon icon = new ImageIcon("C:\\Users\\admin\\Desktop\\worker_guanli\\1.jpg");
JPanel p = newJPanel()
{
protectedvoid paintComponent(Graphics g){
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);}
};
JLabel jluser = new JLabel("使用者名稱:");
JTextField jtuser = new JTextField();
JLabel jlpass = new JLabel("密碼 :");
TextField jtpass = new TextField();
JButton jblogin = new JButton("登入");
JButton jbreset = new JButton("退出");
String user = new String();
String pass = new String();
public dbframe(String s){
super(s);
p.setLayout(null);
jluser.setBounds(new Rectangle(20,30,50,30));
jtuser.setBounds(new Rectangle(80,30,180,30));
jlpass.setBounds(new Rectangle(20,80,50,30));
jtpass.setBounds(new Rectangle(80,80,180,30));
jtpass.setEchoChar('*');
jblogin.setBounds(new Rectangle(80,120,80,30));
jbreset.setBounds(new Rectangle(170,120,80,30));
//p.setBounds(0,0,380,250);
p.setOpaque( false );
p.setPreferredSize( new Dimension(400, 600) );
//this.setLocationRelativeTo( null );
p.add(jluser);
p.add(jtuser);
p.add(jlpass);
p.add(jtpass);
p.add(jblogin);
p.add(jbreset);
//p.add(L2);
//p.setOpaque(false);
this.setContentPane(p);
//this.getLayeredPane().add(L2,-1);
//this.getLayeredPane().add(p,0);
this.setBounds(300,100,340,250);
this.setResizable(false);
this.setVisible(true);
jblogin.addActionListener(this);//實現呼叫
jbreset.addActionListener(this);
}
publicvoid actionPerformed(ActionEvent e){
if(e.getSource()==jblogin){
String u = jtuser.getText();
String p = jtpass.getText();
String name=null;
String pass=null;
String ss = "select * from userr" ;
try{
c.stmt = c.conn.createStatement();
ResultSet rset = c.stmt.executeQuery(ss);
while(rset.next()){
name=rset.getString(1);
pass=rset.getString(2);
}
rset.close();
c.stmt.close();
}catch(Exception a) {System.out.println("查詢使用者出錯!");}
if(u.equals(name)&&p.equals(pass))
{ menue m = new menue("職工考勤資訊管理系統");}
else {JOptionPane.showMessageDialog(null,"使用者名稱或密碼不正確,請重新輸入!");}
}
else System.exit(0);
}
}
(3)主表程式碼
package worker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclassmenueextends JFrame implements ActionListener{ //主選單
JPanel cp= new JPanel();//新增容器JPanel,windos
JButton b1 = new JButton("職工資訊");
JButton b2 = new JButton("出勤資訊");
JButton b3 = new JButton("出差資訊");
JButton b4 = new JButton("請假資訊");
JButton b5 = new JButton("加班資訊");
JButton b6 = new JButton("退出系統");
ImageIcon icon = new ImageIcon("C:\\Users\\admin\\Desktop\\worker_guanli\\2.jpg");
JPanel p = newJPanel()
{
protectedvoid paintComponent(Graphics g){
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);}
};
public menue(String s){
super(s);
p.setLayout(null);
b1.setBounds(new Rectangle(20,30,130,60));//設定按鈕的向右,向下,長,寬
b2.setBounds(new Rectangle(20,130,130,60));
b3.setBounds(new Rectangle(20,230,130,60));
b4.setBounds(new Rectangle(20,330,130,60));
b5.setBounds(new Rectangle(20,430,130,60));
b6.setBounds(new Rectangle(20,530,130,60));
p.add(b1);//新增按鈕
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
this.setVisible(true);//根據()的值來顯示或隱藏
this.setContentPane(p);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(280,50,980,650);//調整容器大小
this.setResizable(false);//可以調整介面大小
p.setOpaque( false );
p.setPreferredSize( new Dimension(400, 600) );
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
publicvoid actionPerformed(ActionEvent e){
if(e.getSource()==b1){
display d = new display(); d.di();}
if(e.getSource()==b2){
chuqin chu = new chuqin();chu.chu();
}
if(e.getSource()==b3){
chuchai chu = new chuchai();chu.chu();
}
if(e.getSource()==b4){
qingjia qing = new qingjia();qing.chu();
}
if(e.getSource()==b5){
jiaban qing = new jiaban();qing.chu();
}
if(e.getSource()==b6){
System.exit(0);
}
}
}
以職工資訊為例::(其餘功能類似)
(4)職工資訊程式碼
package worker;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
importjava.util.*;
importjava.util.Date;
import javax.swing.*;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.applet.*;
importjava.io.*;
class display implements ActionListener{ //(一)職工資訊管理
JFrame f=new JFrame("查詢職工管理");
JButton b1=new JButton("新增");
JButton b2=new JButton("修改");
JButton b3=new JButton("刪除");
JButton b4=new JButton("查詢所有");
JButton b5=new JButton("返回");
JButton b6=new JButton("查詢單人資訊");
JTextField tf1=new JTextField(9);
JTextField tf2=new JTextField(9);
JTextField tf3=new JTextField(9);
JTextField tf4=new JTextField(9);
JTextField tf5=new JTextField(9);
// JTextField tf6=new JTextField(9);
JTextField tf7=new JTextField(9);
String a[]={"男","女"};
JComboBox box = new JComboBox(a);;
String[] cloum={"編號","姓名","性別","年齡","職稱"};
Object[][] row = new Object[50][5];
JTable table=new JTable(row,cloum);
JScrollPane scrollpane=new JScrollPane(table);
JSplitPane splitpane=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane splitpane1=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
void di()
{
JPanel p=(JPanel)f.getContentPane();
//p.setLayout(new FlowLayout());
p.setLayout(new BorderLayout());
p.add(scrollpane);
p.add(splitpane);
p.add(splitpane1);
JPanel p1=new JPanel();
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
JPanel p2=new JPanel();
p2.setBackground(Color.RED);
p2.add(scrollpane);
JPanel p3=new JPanel();
//JPanel p4=new JPanel();
p3.setLayout(new GridLayout(7,2));
// p3.add(new JLabel(""));
p3.add(new JLabel(" 編號:")); p3.add(tf1);
p3.add(new JLabel(" 姓名:")); p3.add(tf2);
p3.add(new JLabel(" 性別:")); p3.add(tf3);
p3.add(new JLabel(" 年齡:")); p3.add(tf4);
p3.add(new JLabel(" 職稱:")); p3.add(tf5);
//p3.add(new JLabel(" 手機號:")); p3.add(tf6);
p3.add(new JLabel(" 在此處輸入職工號點選查詢刪除:")); p3.add(tf7);
//splitpane.setDividerSize(10);
//f.setLayout(new BorderLayout());
p.add(p3,BorderLayout.SOUTH);
splitpane.add(p1,splitpane.TOP);
splitpane.add(p2,splitpane.BOTTOM);
splitpane1.add(splitpane,splitpane1.TOP);
splitpane1.add(p3,splitpane1.BOTTOM);
splitpane.setDividerLocation(50);
splitpane1.setDividerLocation(450);
f.setBounds(300,50,600,660);
f.setResizable(false);//可以調整介面大小
f.setVisible(true);
box.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
publicvoid actionPerformed(ActionEvent e) {
DBConn d = new DBConn("sa","123");
String s1 =tf1.getText();
String s2=tf2.getText();
String s3=tf3.getText();
String s4=tf4.getText();
String s5=tf5.getText();
//String s6=tf6.getText();
//String s3;
if(e.getSource()==b1){
String[] s = {s1,s2,s3,s4,s5};
String ss = "insert into 職工資訊 values(?,?,?,?,?)";
try{
d.prepare = d.conn.prepareStatement(ss);
for(int i=0;i
d.prepare.setString(i+1, s[i]);
d.prepare.execute();
d.prepare.close();
JOptionPane.showMessageDialog(null,"資訊新增 成功!");
tf1.setText(null); tf2.setText(null); tf3.setText(null); tf4.setText(null);
tf5.setText(null); tf7.setText(null);
}catch(Exception q){System.out.println("新增資訊錯誤!");
JOptionPane.showMessageDialog(null,"資訊新增錯誤。可能由於資料格式不正確!");}
}
if(e.getSource()==b2){
System.out.println("name"+s2);
String s11 = "UPDATE 職工資訊 SET 姓名='"+tf2.getText()+"' WHERE 編號='"+tf1.getText()+"'";
String s22 = "UPDATE 職工資訊 SET 性別='"+box.getSelectedItem().toString()+"' WHERE 編號='"+tf1.getText()+"'";
String s33 = "UPDATE 職工資訊 SET 年齡='"+tf4.getText()+"' WHERE 編號='"+tf1.getText()+"'";
String s44 = "UPDATE 職工資訊 SET 職稱='"+tf5.getText()+"' WHERE 編號='"+tf1.getText()+"'";
//String s55 = "UPDATE 職工資訊 SET staff_phone='"+tf6.getText()+"' WHERE staff_id='"+tf1.getText()+"'";
try{
d.stmt = d.conn.createStatement();
d.stmt.executeUpdate(s11);
d.stmt.executeUpdate(s22);
d.stmt.executeUpdate(s33);
d.stmt.executeUpdate(s44);
//d.stmt.executeUpdate(s55);
d.stmt.close();
JOptionPane.showMessageDialog(null,"修改成功功!");
}catch(Exception w){System.out.println(w);JOptionPane.showMessageDialog(null,"修改錯誤。工號不存在!");}
}
if(e.getSource()==b3){
try{
d.stmt = d.conn.createStatement();
d.stmt.execute("DELETE FROM 職工資訊 WHERE 編號="+tf7.getText()+"");
d.stmt.close();
JOptionPane.showMessageDialog(null,"資訊刪除 成功!");
tf1.setText(null); tf2.setText(null); tf4.setText(null);
tf5.setText(null); tf7.setText(null);
}catch(SQLException e1){System.out.println(e1);
JOptionPane.showMessageDialog(null,"刪除錯誤。工號不存在!");}
}
if(e.getSource()==b4){
try{
d.stmt = d.conn.createStatement();
ResultSet rs;
if(b4.equals(e.getSource())){
for(int i=0;i<50;i++)
for(int j=0;j<5;j++)
table.setValueAt("",i,j);
CallableStatement c=d.conn.prepareCall("{call staff_1}");//儲存過程的呼叫
rs=c.executeQuery();
//rs = d.stmt.executeQuery("SELECT * FROM 職工資訊 ORDER BY 編號 ASC");
int k=-1;
while(rs.next()) {
++k;
String id = rs.getString(1);
String name = rs.getString(2);
String sex = rs.getString(3);
String age = rs.getString(4);
String p = rs.getString(5);
//String phone = rs.getString(6);
table.setValueAt(id,k,0);
table.setValueAt(name,k,1);
table.setValueAt(sex,k,2);
table.setValueAt(age,k,3);
table.setValueAt(p,k,4);
//table.setValueAt(phone,k,5);
}
}
}catch(Exception w){System.out.println(w);JOptionPane.showMessageDialog(null,"查詢錯誤,表中無資料!");}
}
if(e.getSource()==b5){
f.dispose();
}
if(e.getSource()==b6){
try {
ResultSet rs;
d.stmt = d.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(b6.equals(e.getSource())){
for(int i=0;i<50;i++)
for(int j=0;j<5;j++)
table.setValueAt("", i, j);
rs = d.stmt.executeQuery("SELECT * FROM 職工資訊 WHERE 編號="+tf7.getText());
int k=-1;
while (rs.next()) {
++k;
String id = rs.getString(1);
String name = rs.getString(2);
String sex = rs.getString(3);
String age = rs.getString(4);
String p = rs.getString(5);
//String phone = rs.getString(6);
table.setValueAt(id,k,0);
table.setValueAt(name,k,1);
table.setValueAt(sex,k,2);
table.setValueAt(age,k,3);
table.setValueAt(p,k,4);
//table.setValueAt(phone,k,5);
}
}
}catch (SQLException e1) {System.out.println(e1);JOptionPane.showMessageDialog(null,"查詢錯誤,表中無資料!");}
}
}
}