码迷,mamicode.com
首页 > 数据库 > 详细

人事管理系统——数据库操作类

时间:2017-05-31 23:13:26      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:ret   ror   mit   jdbc   span   库类   tin   arch   man   

连接数据库类主要代码:

技术分享
  1 package PersonSystem;
  2 
  3 import java.sql.*;
  4 /**
  5  * 
  6  * 连接数据库的类
  7  *
  8  */
  9 public class Database 
 10 {
 11     private Statement stmt = null;
 12     ResultSet rs = null;
 13     private Connection conn = null;
 14     String sql;
 15     String strurl = "jdbc:odbc:HrMS";
 16     
 17     public Database(){
 18     }
 19     /**
 20      * 打开数据库连接
 21      */
 22     public void OpenConn() throws Exception
 23     {
 24         try{
 25             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 26             conn=DriverManager.getConnection(strurl);
 27         }
 28         catch(Exception e){
 29             System.err.println("OpenConn:"+e.getMessage());
 30         }
 31     }
 32     /**
 33      * 执行SQL语句,返回结果集rs
 34      */
 35     public ResultSet executeQuery(String sql)
 36     {
 37         stmt = null;
 38         rs = null;
 39         try{
 40             stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 41             rs=stmt.executeQuery(sql);
 42         }
 43         catch(SQLException e){
 44             System.err.println("executeQuery:"+e.getMessage());
 45         }
 46         return rs;
 47     }
 48     /**
 49      * 执行SQL语句
 50      */
 51     public void executeUpdate(String sql)
 52     {
 53         stmt = null;
 54         rs = null;
 55         try{
 56             stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 57             rs=stmt.executeQuery(sql);
 58             conn.commit();
 59         }
 60         catch(SQLException e){
 61             System.err.println("executeUpdate:"+e.getMessage());
 62         }
 63     }
 64     public void closeStmt()
 65     {
 66         try{
 67             stmt.close();
 68         }
 69         catch(SQLException e){
 70             System.err.println("closeStmt:"+e.getMessage());
 71         }
 72     }
 73     /**
 74      * 关闭数据库连接
 75      */
 76     public void closeConn(){
 77         try{
 78             conn.close();
 79         }
 80         catch(SQLException ex){
 81             System.err.println("aq.closeConn:"+ex.getMessage());
 82         }
 83     }
 84     /**
 85      * 转换编码
 86      */
 87     public static String toGBK(String str)
 88     {
 89         try{
 90             if(str==null)
 91                 str = "";
 92             else
 93                 str = new String(str.getBytes("ISO-8859-1"),"GBK");
 94         }
 95         catch(Exception e){
 96             System.out.println(e);
 97         }
 98         return str;
 99     }
100 }
1

 有关人员信息数据库操作的类主要代码:

技术分享
  1 package PersonSystem;
  2 
  3 //import java.util.*;
  4 import java.sql.*;
  5 import javax.swing.*;
  6 /**
  7  * 
  8  * 有关人员信息数据库操作的类
  9  *
 10  */
 11 public class PersonBean 
 12 {
 13     String sql;
 14     ResultSet rs = null;
 15     
 16     String field1;   //PersonID
 17     String field2;   //Name
 18     String field3;   //Sex
 19     String field4;   //Birth
 20     String field5;   //Nat
 21     String field6;   //Address
 22     String field7;   //DeptID
 23     String field8;   //Salary
 24     String field9;   //Assess
 25     String field10;  //Other
 26     
 27     String colName;  //列名
 28     String colValue;  //列值
 29     String colValue2;  //列值
 30     
 31     /**
 32      * 添加信息
 33      */
 34     public void add(String f1,String f2,String f3,String f4,String f5,
 35                     String f6,String f7,String f8,String f9,String f10)
 36     {
 37         Database DB = new Database();
 38         
 39         this.field1 = f1;
 40         this.field2 = f2;
 41         this.field3 = f3;
 42         this.field4 = f4;
 43         this.field5 = f5;
 44         this.field6 = f6;
 45         this.field7 = f7;
 46         this.field8 = f8;
 47         this.field9 = f9;
 48         this.field10 = f10;
 49         
 50         if(field2 == null||field2.equals("")){
 51             JOptionPane.showMessageDialog(null, "请输入员工姓名","错误",JOptionPane.ERROR_MESSAGE);
 52             return;
 53         }
 54         else if(field3 == null||field3.equals("")){
 55             JOptionPane.showMessageDialog(null, "请输入性别","错误",JOptionPane.ERROR_MESSAGE);
 56             return;
 57         }
 58         else if(field4 == null||field4.equals("")){
 59             JOptionPane.showMessageDialog(null, "请输入出生年月","错误",JOptionPane.ERROR_MESSAGE);
 60             return;
 61         }
 62         else if(field5 == null||field5.equals("")){
 63             JOptionPane.showMessageDialog(null, "请输入民族","错误",JOptionPane.ERROR_MESSAGE);
 64             return;
 65         }
 66         else if(field6 == null||field6.equals("")){
 67             JOptionPane.showMessageDialog(null, "请输入地址","错误",JOptionPane.ERROR_MESSAGE);
 68             return;
 69         }
 70         else if(field7 == null||field7.equals("")){
 71             JOptionPane.showMessageDialog(null, "请输入部门","错误",JOptionPane.ERROR_MESSAGE);
 72             return;
 73         }
 74         else if(field8 == null||field8.equals("")){
 75             JOptionPane.showMessageDialog(null, "请输入薪酬","错误",JOptionPane.ERROR_MESSAGE);
 76             return;
 77         }
 78         else{
 79             sql = "insert into Person(PersonID,Name,Sex,Birth,Nat,Address,DeptID,Salary,Assess,Other)"
 80                 +"values(‘"+field1+"‘,‘"+field2+"‘,‘"+field3+"‘,‘"+field4+"‘,‘"+field5+"‘,"
 81                 + "‘"+field6+"‘,‘"+field7+"‘,‘"+field8+"‘,‘"+field9+"‘,‘"+field10+"‘)";
 82             try{
 83                 DB.OpenConn();
 84                 DB.executeUpdate(sql);
 85                 JOptionPane.showMessageDialog(null, "成功添加一条记录!");
 86             }
 87             catch(Exception e){
 88                 System.out.println(e);
 89                 JOptionPane.showMessageDialog(null, "保存失败","错误",JOptionPane.ERROR_MESSAGE);
 90             }
 91             finally{
 92                 DB.closeStmt();
 93                 DB.closeConn();
 94             }
 95         }
 96     }
 97     
 98     /**
 99      * 修改信息
100      */
101     public void modify(String f1,String f2,String f3,String f4,String f5,
102                     String f6,String f7,String f8,String f9,String f10)
103     {
104         Database DB = new Database();
105         
106         this.field1 = f1;
107         this.field2 = f2;
108         this.field3 = f3;
109         this.field4 = f4;
110         this.field5 = f5;
111         this.field6 = f6;
112         this.field7 = f7;
113         this.field8 = f8;
114         this.field9 = f9;
115         this.field10 = f10;
116         
117         if(field2 == null||field2.equals("")){
118             JOptionPane.showMessageDialog(null, "请输入员工姓名","错误",JOptionPane.ERROR_MESSAGE);
119             return;
120         }
121         else if(field3 == null||field3.equals("")){
122             JOptionPane.showMessageDialog(null, "请输入性别","错误",JOptionPane.ERROR_MESSAGE);
123             return;
124         }
125         else if(field4 == null||field4.equals("")){
126             JOptionPane.showMessageDialog(null, "请输入出生年月","错误",JOptionPane.ERROR_MESSAGE);
127             return;
128         }
129         else if(field5 == null||field5.equals("")){
130             JOptionPane.showMessageDialog(null, "请输入民族","错误",JOptionPane.ERROR_MESSAGE);
131             return;
132         }
133         else if(field6 == null||field6.equals("")){
134             JOptionPane.showMessageDialog(null, "请输入地址","错误",JOptionPane.ERROR_MESSAGE);
135             return;
136         }
137         else if(field7 == null||field7.equals("")){
138             JOptionPane.showMessageDialog(null, "请输入部门","错误",JOptionPane.ERROR_MESSAGE);
139             return;
140         }
141         else if(field8 == null||field8.equals("")){
142             JOptionPane.showMessageDialog(null, "请输入薪酬","错误",JOptionPane.ERROR_MESSAGE);
143             return;
144         }
145         else{
146             sql = "update Person set Name= ‘"+field2+"‘,"+"Sex=‘"+field3+"‘,‘"+"Birth=‘"+field4+"‘,‘"
147                  +"Nat=‘"+field5+"‘,‘"+"Address=‘"+field6+"‘,‘"+"‘"+"DeptID=‘"+field7+"‘,‘"+"Salary=‘"+field8+"‘,‘"
148                     +"Assess=‘"+field9+"‘,‘"+"Other=‘"+field10+"‘,‘"+"where PersonID=‘"+field1+"";
149             try{
150                 DB.OpenConn();
151                 DB.executeUpdate(sql);
152                 JOptionPane.showMessageDialog(null, "成功修改一条新的记录!");
153             }
154             catch(Exception e){
155                 System.out.println(e);
156                 JOptionPane.showMessageDialog(null, "更新失败","错误",JOptionPane.ERROR_MESSAGE);
157             }
158             finally{
159                 DB.closeStmt();
160                 DB.closeConn();
161             }
162         }
163     }
164     /**
165      * 删除信息
166      */
167     public void delete(String f1)
168     {
169         Database DB = new Database();
170         this.field1 = f1;
171         
172         sql = "delect from Person where PersonID ="+field1+"";
173         try{
174             DB.OpenConn();
175             DB.executeUpdate(sql);
176             JOptionPane.showMessageDialog(null, "成功删除一条记录!");
177         }
178         catch(Exception e){
179             System.out.println(e);
180             JOptionPane.showMessageDialog(null, "删除失败","错误",JOptionPane.ERROR_MESSAGE);
181         }
182         finally{
183             DB.closeStmt();
184             DB.closeConn();
185         }
186     }
187     /**
188      * 根据编号查询信息
189      */
190     public String[] search(String f1)
191     {
192         Database DB = new Database();
193         this.field1 = f1;
194         String[] s = new String[10];
195         sql = "select * from Person where PersonID = "+field1+"";
196         try{
197             DB.OpenConn();
198             DB.executeUpdate(sql);
199             if(rs.next()){
200                 s[0] = rs.getString("PersonID");
201                 s[1] = rs.getString("Name");
202                 s[2] = rs.getString("Sex");
203                 s[3] = rs.getString("Birth");
204                 s[4] = rs.getString("Nat");
205                 s[5] = rs.getString("Address");
206                 s[6] = rs.getString("DeptID");
207                 s[7] = rs.getString("Salary");
208                 s[8] = rs.getString("Assess");
209                 s[9] = rs.getString("Other");
210             }
211             else
212                 s = null;
213         }
214         catch(Exception e){
215         }
216         finally{
217             DB.closeStmt();
218             DB.closeConn();
219         }
220         return s;
221     }
222     /**
223      * 人员记录综合查询(查询所有记录)
224      */
225     public String[][] searchAllForNode()
226     {
227         Database DB = new Database();
228         String[][] sn = null;
229         int row = 0;
230         int i = 0;
231         sql = "select PersonID,Name,Sex,Dept.DeptID as DeptID,B_Dept,S_Dept,Salary,Assess "
232                 + "from Dept,Person where Dept.DeptID = Person.DeptID order by PersonID";
233         try{
234             DB.OpenConn();
235             rs = DB.executeQuery(sql);
236             if(rs.last()){
237                 row = rs.getRow();
238             }
239             if(row==0){
240                 sn = new String[1][6];
241                 sn[0][0] = " ";
242                 sn[0][1] = " ";
243                 sn[0][2] = " ";
244                 sn[0][3] = " ";
245                 sn[0][4] = " ";
246                 sn[0][5] = " ";
247             }
248             else{
249                 sn = new String[row][6];
250                 rs.first();
251                 rs.previous();
252                 while(rs.next()){
253                     sn[i][0] = rs.getString("PersonID");
254                     sn[i][1] = rs.getString("Name");
255                     sn[i][2] = rs.getString("Sex");
256                     sn[i][3] = rs.getString("B_Dept")+"-"+rs.getString("S_Dept");
257                     sn[i][4] = rs.getString("Salary");
258                     sn[i][5] = rs.getString("Assess");
259                     i++;
260                 }
261             }
262         }
263         catch(Exception e){
264             
265         }
266         finally{
267             DB.closeStmt();
268             DB.closeConn();
269         }
270         return sn;
271     }
272     /**
273      * 修改信息
274      */
275     public void updateDept(String f1,String f7)
276     {
277         Database DB = new Database();
278         this.field1 = f1;
279         this.field7 = f7;
280         
281         sql = "update Person set DeptID = "+field7+"where PersonID ="+field1;
282         try{
283             DB.OpenConn();
284             DB.executeUpdate(sql);
285             JOptionPane.showMessageDialog(null, "人员调动成功!");
286         }
287         catch(Exception e){
288             System.out.println(e);
289             JOptionPane.showMessageDialog(null, "更新失败","错误",JOptionPane.ERROR_MESSAGE);
290         }
291         finally{
292             DB.closeStmt();
293             DB.closeConn();
294         }
295     }
296     /**
297      * 修改信息
298      */
299     public void updateSalary(String f1,String f8)
300     {
301         Database DB = new Database();
302         this.field1 = f1;
303         this.field8 = f8;
304         
305         sql = "update Person set Salary =‘"+field8+"‘where PersonID ="+field1;
306         try{
307             DB.OpenConn();
308             DB.executeUpdate(sql);
309             JOptionPane.showMessageDialog(null, "劳资更改成功!");
310         }
311         catch(Exception e){
312             System.out.println(e);
313             JOptionPane.showMessageDialog(null, "更新失败","错误",JOptionPane.ERROR_MESSAGE);
314         }
315         finally{
316             DB.closeStmt();
317             DB.closeConn();
318         }
319     }
320     /**
321      * 修改信息
322      */
323     public void updateAssess(String f1,String f9)
324     {
325         Database DB = new Database();
326         this.field1 = f1;
327         this.field8 = f9;
328         
329         sql = "update Person set Assess =‘"+field9+"‘where PersonID ="+field1;
330         try{
331             DB.OpenConn();
332             DB.executeUpdate(sql);
333             JOptionPane.showMessageDialog(null, "人员考核成功!");
334         }
335         catch(Exception e){
336             System.out.println(e);
337             JOptionPane.showMessageDialog(null, "更新失败","错误",JOptionPane.ERROR_MESSAGE);
338         }
339         finally{
340             DB.closeStmt();
341             DB.closeConn();
342         }
343     }
344     /**
345      * 人员信息综合查询
346      */
347     public String[][] searchAll()
348     {
349         Database DB = new Database();
350         String[][] sn = null;
351         int row = 0;
352         int i = 0;
353         sql = "select * from Person order by PersonID";
354         try{
355             DB.OpenConn();
356             rs = DB.executeQuery(sql);
357             if(rs.last()){
358                 row = rs.getRow();
359             }
360             if(row==0){
361                 sn = new String[1][6];
362                 sn[0][0] = " ";
363                 sn[0][1] = " ";
364                 sn[0][2] = " ";
365                 sn[0][3] = " ";
366                 sn[0][4] = " ";
367                 sn[0][5] = " ";
368             }
369             else{
370                 sn = new String[row][6];
371                 rs.first();
372                 rs.previous();
373                 while(rs.next()){
374                     sn[i][0] = rs.getString("PersonID");
375                     sn[i][1] = rs.getString("Name");
376                     sn[i][2] = rs.getString("Sex");
377                     sn[i][3] = rs.getString("Nat");
378                     sn[i][4] = rs.getString("Address");
379                     DeptBean dp = new DeptBean();
380                     sn[i][5] = dp.getDeptStr(rs.getString("DeptID"));
381                     i++;
382                 }
383             }
384         }
385         catch(Exception e){
386             
387         }
388         finally{
389             DB.closeStmt();
390             DB.closeConn();
391         }
392         return sn;
393     }
394     /**
395      * 获得新的ID
396      */
397     public int getId()
398     {
399         Database DB = new Database();
400         int ID = 1;
401         sql = "select max(PersonID) from Person";
402         try{
403             DB.OpenConn();
404             rs = DB.executeQuery(sql);
405             if(rs.next()){
406                 ID = rs.getInt(1) + 1;
407             }
408             else
409                 ID = 1;
410         }
411         catch(Exception e){
412             
413         }
414         finally{
415             DB.closeStmt();
416             DB.closeConn();
417         }
418         return ID;
419     }
420     /**
421      * 取得DeptID
422      */
423     public String getDeptId(String f1)
424     {
425         Database DB = new Database();
426         sql = "select DeptID from Person where personID = "+f1;
427         String deptid = null;
428         try{
429             DB.OpenConn();
430             rs = DB.executeQuery(sql);
431             if(rs.next()){
432                 deptid = rs.getString("DeptID");
433             }
434             else
435                 deptid = "";
436         }
437         catch(Exception e){
438             
439         }
440         finally{
441             DB.closeStmt();
442             DB.closeConn();
443         }
444         return deptid;
445     }
446     /**
447      * 取得Name
448      */
449     public String getName(String f1)
450     {
451         Database DB = new Database();
452         sql = "select Name from Person where personID = "+f1;
453         String name = null;
454         try{
455             DB.OpenConn();
456             rs = DB.executeQuery(sql);
457             if(rs.next()){
458                 name = rs.getString("DeptID");
459             }
460             else
461                 name = "";
462         }
463         catch(Exception e){
464             
465         }
466         finally{
467             DB.closeStmt();
468             DB.closeConn();
469         }
470         return name;
471     }
472     /**
473      * 获得表中的所有编号
474      */
475     public String[] getAllId()
476     {
477         String[] s = null;
478         int row = 0;
479         int i = 0;
480         Database DB = new Database();
481         sql = "select PersonID,name from Person order by PersonID";
482         try{
483             DB.OpenConn();
484             rs = DB.executeQuery(sql);
485             if(rs.last()){
486                 row = rs.getRow();
487             }
488             if(row == 0){
489                 s = null;
490             }
491             else{
492                 s = new String[row];
493                 rs.first();
494                 rs.previous();
495                 while(rs.next()){
496                     s[i] = rs.getString(1)+"-"+rs.getShort(2);
497                     i++;
498                 }
499             }
500         }
501         catch(Exception e){
502             System.out.println(e);
503         }
504         finally{
505             DB.closeStmt();
506             DB.closeConn();
507         }
508         return s;
509     }
510 }
2

有关部门信息数据库操作的类主要代码:

技术分享
  1 package PersonSystem;
  2 
  3 //import java.util.*;
  4 import java.sql.*;
  5 import javax.swing.*;
  6 
  7 /**
  8  * 
  9  * 有关部门信息数据库操作的类
 10  *
 11  */
 12 public class DeptBean
 13 {
 14     String sql;
 15     ResultSet rs = null;
 16     
 17     String field1;   //DeptID
 18     String field2;   //B_Dept
 19     String field3;   //S_Dept
 20     
 21     String colName;  //列名
 22     String colValue;  //列值
 23     String colValue2;  //列值
 24     
 25     /**
 26      * 添加信息
 27      */
 28     public void add(String f1,String f2,String f3)
 29     {
 30         Database DB = new Database();
 31         
 32         this.field1 = f1;
 33         this.field2 = f2;
 34         this.field3 = f3;
 35         
 36         if(field2 == null||field2.equals("")){
 37             JOptionPane.showMessageDialog(null, "请输入一级部门名称","错误",JOptionPane.ERROR_MESSAGE);
 38             return;
 39         }
 40         else if(field3 == null||field3.equals("")){
 41             JOptionPane.showMessageDialog(null, "请输入二级部门名称","错误",JOptionPane.ERROR_MESSAGE);
 42             return;
 43         }
 44         else{
 45             sql = "insert into Dept(DeptID,B_Dept,S_Dept) values(‘"+field1+"‘,‘"+field2+"‘,‘"+field3+"‘)";
 46             try{
 47                 DB.OpenConn();
 48                 DB.executeUpdate(sql);
 49                 JOptionPane.showMessageDialog(null, "成功添加一条新的记录!");
 50             }
 51             catch(Exception e){
 52                 System.out.println(e);
 53                 JOptionPane.showMessageDialog(null, "保存失败","错误",JOptionPane.ERROR_MESSAGE);
 54             }
 55             finally{
 56                 DB.closeStmt();
 57                 DB.closeConn();
 58             }
 59         }
 60     }
 61     /**
 62      * 修改信息
 63      */
 64     public void modify(String f1,String f2,String f3)
 65     {
 66         Database DB = new Database();
 67         
 68         this.field1 = f1;
 69         this.field2 = f2;
 70         this.field3 = f3;
 71         
 72         if(field2 == null||field2.equals("")){
 73             JOptionPane.showMessageDialog(null, "请输入一级部门名称","错误",JOptionPane.ERROR_MESSAGE);
 74             return;
 75         }
 76         else if(field3 == null||field3.equals("")){
 77             JOptionPane.showMessageDialog(null, "请输入二级部门名称","错误",JOptionPane.ERROR_MESSAGE);
 78             return;
 79         }
 80         else{
 81             sql = "update Dept set B_Dept = ‘"+field2+"‘,S_Dept = ‘"+field3+"‘ where DeptID = "+field1+"";
 82             try{
 83                 DB.OpenConn();
 84                 DB.executeUpdate(sql);
 85                 JOptionPane.showMessageDialog(null, "成功修改一条新的记录!");
 86             }
 87             catch(Exception e){
 88                 System.out.println(e);
 89                 JOptionPane.showMessageDialog(null, "更新失败","错误",JOptionPane.ERROR_MESSAGE);
 90             }
 91             finally{
 92                 DB.closeStmt();
 93                 DB.closeConn();
 94             }
 95         }
 96     }
 97     /**
 98      * 删除信息
 99      */
100     public void delete(String f1)
101     {
102         Database DB = new Database();
103         this.field1 = f1;
104         
105         sql = "delect from Dept where DeptID ="+field1+"";
106         try{
107             DB.OpenConn();
108             DB.executeUpdate(sql);
109             JOptionPane.showMessageDialog(null, "成功删除一条记录!");
110         }
111         catch(Exception e){
112             System.out.println(e);
113             JOptionPane.showMessageDialog(null, "删除失败","错误",JOptionPane.ERROR_MESSAGE);
114         }
115         finally{
116             DB.closeStmt();
117             DB.closeConn();
118         }
119     }
120     /**
121      * 根据编号查询信息
122      */
123     public String[] search(String f1)
124     {
125         Database DB = new Database();
126         this.field1 = f1;
127         String[] s = new String[10];
128         sql = "select * from Dept where DeptID = "+field1+"";
129         try{
130             DB.OpenConn();
131             DB.executeUpdate(sql);
132             if(rs.next()){
133                 s[0] = rs.getString("DeptID");
134                 s[1] = rs.getString("B_Dept");
135                 s[2] = rs.getString("S_Dept");
136             }
137             else
138                 s = null;
139         }
140         catch(Exception e){
141         }
142         finally{
143             DB.closeStmt();
144             DB.closeConn();
145         }
146         return s;
147     }
148     /**
149      * 查询所有记录
150      */
151     public String[][] searchAll()
152     {
153         Database DB = new Database();
154         String[][] sn = null;
155         int row = 0;
156         int i = 0;
157         sql = "select * from Dept order by DeptID";
158         try{
159             DB.OpenConn();
160             rs = DB.executeQuery(sql);
161             if(rs.last()){
162                 row = rs.getRow();
163             }
164             if(row==0){
165                 sn = new String[1][3];
166                 sn[0][0] = " ";
167                 sn[0][1] = " ";
168                 sn[0][2] = " ";
169             }
170             else{
171                 sn = new String[row][3];
172                 rs.first();
173                 rs.previous();
174                 while(rs.next()){
175                     sn[i][0] = rs.getString("DeptID");
176                     sn[i][1] = rs.getString("B_Dept");
177                     sn[i][2] = rs.getString("S_Dept");
178                     i++;
179                 }
180             }
181         }
182         catch(Exception e){
183             
184         }
185         finally{
186             DB.closeStmt();
187             DB.closeConn();
188         }
189         return sn;
190     }
191     /**
192      * 为人事管理提供查询
193      */
194     @SuppressWarnings("null")
195     public String[] searchAllForNode()
196     {
197         Database DB = new Database();
198         String[] sn = null;
199         int row = 0;
200         int i = 0;
201         sql = "select * from Dept order by DeptID";
202         try{
203             DB.OpenConn();
204             rs = DB.executeQuery(sql);
205             if(rs.last()){
206                 row = rs.getRow();
207             }
208             if(row==0){
209                 sn[0] = "";
210                 sn[1] = "";
211                 sn[2] = "";
212             }
213             else{
214                 sn = new String[row];
215                 rs.first();
216                 rs.previous();
217                 while(rs.next()){
218                     sn[i] = rs.getString("DeptID")+"-"+rs.getString("B_Dept")+"-"+rs.getString("S_Dept");
219                     i++;
220                 }
221             }
222         }
223         catch(Exception e){
224             
225         }
226         finally{
227             DB.closeStmt();
228             DB.closeConn();
229         }
230         return sn;
231     }
232     /**
233      * 人员记录综合查询(按ID查询)
234      */
235     public String[][] searchAll(String f1)
236     {
237         this.field1 = f1;
238         Database DB = new Database();
239         String[][] sn = null;
240         int row = 0;
241         int i = 0;
242         sql = "select * from Dept where DeptID="+field1+" order by DeptID";
243         try{
244             DB.OpenConn();
245             rs = DB.executeQuery(sql);
246             if(rs.last()){
247                 row = rs.getRow();
248             }
249             if(row==0){
250                 sn = null;
251             }
252             else{
253                 sn = new String[row][6];
254                 rs.first();
255                 rs.previous();
256                 while(rs.next()){
257                     sn[i][0] = rs.getString("DeptID");
258                     sn[i][1] = rs.getString("B_Dept");
259                     sn[i][2] = rs.getString("S_Dept");
260                     i++;
261                 }
262             }
263         }
264         catch(Exception e){
265             
266         }
267         finally{
268             DB.closeStmt();
269             DB.closeConn();
270         }
271         return sn;
272     }
273     /**
274      * 获得新的ID
275      */
276     public int getId()
277     {
278         Database DB = new Database();
279         int ID = 1;
280         sql = "select max(DeptID) from Dept";
281         try{
282             DB.OpenConn();
283             rs = DB.executeQuery(sql);
284             if(rs.next()){
285                 ID = rs.getInt(1) + 1;
286             }
287             else
288                 ID = 1;
289         }
290         catch(Exception e){
291             
292         }
293         finally{
294             DB.closeStmt();
295             DB.closeConn();
296         }
297         return ID;
298     }
299     /**
300      * 获得表中的所有编号
301      */
302     public String[] getAllId()
303     {
304         String[] s = null;
305         int row = 0;
306         int i = 0;
307         Database DB = new Database();
308         sql = "select DeptID from DeptType order by DeptID";
309         try{
310             DB.OpenConn();
311             rs = DB.executeQuery(sql);
312             if(rs.last()){
313                 row = rs.getRow();
314             }
315             if(row == 0){
316                 s = null;
317             }
318             else{
319                 s = new String[row];
320                 rs.first();
321                 rs.previous();
322                 while(rs.next()){
323                     s[i] = rs.getString(1);
324                     i++;
325                 }
326             }
327         }
328         catch(Exception e){
329             System.out.println(e);
330         }
331         finally{
332             DB.closeStmt();
333             DB.closeConn();
334         }
335         return s;
336     }
337     /**
338      * 根据编号查询信息
339      */
340     public String getDeptStr(String f1)
341     {
342         Database DB = new Database();
343         this.field1 = f1;
344         String s = "";
345         sql = "select * from Dept where DeptID ="+field1+"";
346         try{
347             DB.OpenConn();
348             rs = DB.executeQuery(sql);
349             if(rs.next()){
350                 s = rs.getString("B_Dept")+"-"+rs.getString("S_Dept");
351             }
352             else
353                 s = null;
354         }
355         catch(Exception e){
356             
357         }
358         finally{
359             DB.closeStmt();
360             DB.closeConn();
361         }
362         return s;
363     }
364 }
3

有历史流水数据库操作的类主要代码:

技术分享
  1 package PersonSystem;
  2 
  3 //import java.util.*;
  4 import java.sql.*;
  5 import javax.swing.*;
  6 import java.text.DateFormat;
  7 
  8 /**
  9 * 
 10 * 有历史流水数据库操作的类
 11 *
 12 */
 13 public class HistrjnBean 
 14 {
 15     String sql;
 16     ResultSet rs = null;
 17     
 18     String field1;   //JourNo
 19     String field2;   //FromAcc
 20     String field3;   //OldInfo
 21     String field4;   //NewInfo
 22     String field5;   //ChgTime
 23     String field6;   //RegDate
 24     String field7;   //PersonID
 25     
 26     String colName;  //列名
 27     String colValue;  //列值
 28     String colValue2;  //列值
 29     
 30     /**
 31      * 添加信息
 32      */
 33     public void add(String f1,String f2,String f3,String f4,String f5,String f6,String f7)
 34     {
 35         Database DB = new Database();
 36         
 37         this.field1 = f1;
 38         this.field2 = f2;
 39         this.field3 = f3;
 40         this.field4 = f4;
 41         this.field5 = f5;
 42         this.field6 = f6;
 43         this.field7 = f7;
 44         
 45         sql = "insert into Histrjn(JourNo,FromAcc,OldInfo,NewInfo,ChgTime,RegDate,PersonID)"
 46                 +"values(‘"+field1+"‘,‘"+field2+"‘,‘"+field3+"‘,‘"+field4+"‘,‘"+field5+"‘,"
 47                 + "‘"+field6+"‘,‘"+field7+"‘)";
 48         try{
 49             DB.OpenConn();
 50             DB.executeUpdate(sql);
 51         }
 52         catch(Exception e){
 53             System.out.println(e);
 54             JOptionPane.showMessageDialog(null, "保存失败","错误",JOptionPane.ERROR_MESSAGE);
 55         }
 56         finally{
 57             DB.closeStmt();
 58             DB.closeConn();
 59         }
 60     }
 61     /**
 62      * 查询所有记录
 63      */
 64     public String[][] searchAllForDept()
 65     {
 66         Database DB = new Database();
 67         DeptBean deptBean = new DeptBean();
 68         
 69         String[][] sn = null;
 70         int row = 0;
 71         int i = 0;
 72         sql = "select * from Histrjn,Person where Fromacc=‘人员调动‘ and "
 73                 + "Person.PersonID=Histrjn.PersonID order by Histrjn.PersonID,ChgTime";
 74         try{
 75             DB.OpenConn();
 76             rs = DB.executeQuery(sql);
 77             if(rs.last()){
 78                 row = rs.getRow();
 79             }
 80             if(row==0){
 81                 sn = new String[1][6];
 82                 sn[0][0] = " ";
 83                 sn[0][1] = " ";
 84                 sn[0][2] = " ";
 85                 sn[0][3] = " ";
 86                 sn[0][4] = " ";
 87                 sn[0][5] = " ";
 88             }
 89             else{
 90                 sn = new String[row][6];
 91                 rs.first();
 92                 rs.previous();
 93                 while(rs.next()){
 94                     sn[i][0] = rs.getString("JourNo");
 95                     sn[i][1] = rs.getString("Name");
 96                     sn[i][2] = deptBean.getDeptStr(rs.getString("OldInfo"));
 97                     sn[i][3] = deptBean.getDeptStr(rs.getString("NewInfo"));
 98                     sn[i][4] = rs.getString("ChgTime");
 99                     sn[i][5] = rs.getString("RegDate");
100                     i++;
101                 }
102             }
103         }
104         catch(Exception e){
105             
106         }
107         finally{
108             DB.closeStmt();
109             DB.closeConn();
110         }
111         return sn;
112     }
113     /**
114      * 查询所有记录
115      */
116     public String[][] searchAllForSalary()
117     {
118         Database DB = new Database();
119         
120         String[][] sn = null;
121         int row = 0;
122         int i = 0;
123         sql = "select * from Histrjn,Person where Fromacc=‘劳资分配‘ and "
124                 + "Person.PersonID=Histrjn.PersonID order by Histrjn.PersonID,ChgTime";
125         try{
126             DB.OpenConn();
127             rs = DB.executeQuery(sql);
128             if(rs.last()){
129                 row = rs.getRow();
130             }
131             if(row==0){
132                 sn = new String[1][6];
133                 sn[0][0] = " ";
134                 sn[0][1] = " ";
135                 sn[0][2] = " ";
136                 sn[0][3] = " ";
137                 sn[0][4] = " ";
138                 sn[0][5] = " ";
139             }
140             else{
141                 sn = new String[row][6];
142                 rs.first();
143                 rs.previous();
144                 while(rs.next()){
145                     sn[i][0] = rs.getString("JourNo");
146                     sn[i][1] = rs.getString("Name");
147                     sn[i][2] = rs.getString("OldInfo");
148                     sn[i][3] = rs.getString("NewInfo");
149                     sn[i][4] = rs.getString("ChgTime");
150                     sn[i][5] = rs.getString("RegDate");
151                     i++;
152                 }
153             }
154         }
155         catch(Exception e){
156             
157         }
158         finally{
159             DB.closeStmt();
160             DB.closeConn();
161         }
162         return sn;
163     }
164     /**
165      * 查询所有记录
166      */
167     public String[][] searchAllForAssess()
168     {
169         Database DB = new Database();
170         
171         String[][] sn = null;
172         int row = 0;
173         int i = 0;
174         sql = "select * from Histrjn,Person where Fromacc=‘人员考核‘ and "
175                 + "Person.PersonID=Histrjn.PersonID order by Histrjn.PersonID,ChgTime";
176         try{
177             DB.OpenConn();
178             rs = DB.executeQuery(sql);
179             if(rs.last()){
180                 row = rs.getRow();
181             }
182             if(row==0){
183                 sn = new String[1][6];
184                 sn[0][0] = " ";
185                 sn[0][1] = " ";
186                 sn[0][2] = " ";
187                 sn[0][3] = " ";
188                 sn[0][4] = " ";
189                 sn[0][5] = " ";
190             }
191             else{
192                 sn = new String[row][6];
193                 rs.first();
194                 rs.previous();
195                 while(rs.next()){
196                     sn[i][0] = rs.getString("JourNo");
197                     sn[i][1] = rs.getString("Name");
198                     sn[i][2] = rs.getString("OldInfo");
199                     sn[i][3] = rs.getString("NewInfo");
200                     sn[i][4] = rs.getString("ChgTime");
201                     sn[i][5] = rs.getString("RegDate");
202                     i++;
203                 }
204             }
205         }
206         catch(Exception e){
207             
208         }
209         finally{
210             DB.closeStmt();
211             DB.closeConn();
212         }
213         return sn;
214     }
215     /**
216      * 获得新的ID
217      */
218     public int getId()
219     {
220         Database DB = new Database();
221         int ID = 1;
222         sql = "select max(JourNo) from Histrjn";
223         try{
224             DB.OpenConn();
225             rs = DB.executeQuery(sql);
226             if(rs.next()){
227                 ID = rs.getInt(1) + 1;
228             }
229             else
230                 ID = 1;
231         }
232         catch(Exception e){
233             
234         }
235         finally{
236             DB.closeStmt();
237             DB.closeConn();
238         }
239         return ID;
240     }
241     /**
242      * 获得新的ID
243      */
244     public int getChgTime(String f2,String f7)
245     {
246         Database DB = new Database();
247         int ID = 1;
248         sql = "select max(ChgTime) from Histrjn where FromAcc=‘"+f2+"‘ and PersonID="+f7;
249         System.out.println("sql="+sql);
250         try{
251             DB.OpenConn();
252             rs = DB.executeQuery(sql);
253             if(rs.next()){
254                 ID = rs.getInt(1) + 1;
255             }
256             else
257                 ID = 1;
258         }
259         catch(Exception e){
260             
261         }
262         finally{
263             DB.closeStmt();
264             DB.closeConn();
265         }
266         return ID;
267     }
268     /**
269      * 判断是否有记录
270      */
271     public boolean isRows(String f7)
272     {
273         Database DB = new Database();
274         
275         boolean have = true;
276         sql = "select * from Histrjn where PersonID="+f7;
277         try{
278             DB.OpenConn();
279             rs = DB.executeQuery(sql);
280             if(rs.next()){
281                 have = false;
282             }
283         }
284         catch(Exception e){
285             
286         }
287         finally{
288             DB.closeStmt();
289             DB.closeConn();
290         }
291         return have;
292     }
293 }
4

 

人事管理系统——数据库操作类

标签:ret   ror   mit   jdbc   span   库类   tin   arch   man   

原文地址:http://www.cnblogs.com/wxywxy/p/6926070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!