Database.java~102~
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

  1. package manpowersystem;
  2. /**
  3.  * <p>Title: </p>
  4.  * <p>Description: </p>
  5.  * <p>Copyright: Copyright (c) 2003</p>
  6.  * <p>Company: </p>
  7.  * @author not attributable
  8.  * @version 1.0
  9.  */
  10. import java.sql.*;
  11. import java.util.*;
  12. public class Database {
  13.     Connection conn = null;
  14.     ResultSet rs = null;
  15.     Statement stmt = null;
  16.     public RecordItem[] AccessData() throws Exception {
  17.         Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
  18.         String url = "jdbc:microsoft:sqlserver://localhost:1433;" +
  19.             "DatabaseName=Manpower";
  20.         conn = DriverManager.getConnection( url, "sa", "" );
  21.         stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
  22.                                      ResultSet.CONCUR_UPDATABLE );
  23.         String SQL = "select * from WorkTime";
  24.         rs = stmt.executeQuery( SQL );
  25.         RecordItem[] item = new RecordItem[ 100 ];
  26.         for ( int j = 0; j < 100; j++ )
  27.             item[ j ] = new RecordItem();
  28.         int i = 0;
  29.         while ( rs.next() && ( i < 100 ) ) {
  30.             item[ i ].SetEmployeeID( rs.getString( "EmployeeID" ) );
  31.             item[ i ].SetEmployeeName( rs.getString( "EmployeeName" ) );
  32.             item[ i ].SetOnWorkTime(rs.getString( "OnworkTime" ) );
  33.             item[ i ].SetOffWorkTime( rs.getString( "OffworkTime" ) );
  34.             item[ i ].SetLeaveWorkTime( rs.getString( "LeaveworkTime" ) );
  35.             item[ i ].SetDescribe( rs.getString( "Describe" ) );
  36.             i++;
  37.         }
  38.         RecordItem[] result = new RecordItem[ i ];
  39.         for ( int j = 0; j < i; j++ ) {
  40.             result[ j ] = new RecordItem();
  41.             result[ j ] = item[ j ];
  42.         }
  43.         rs.close();
  44.         stmt.close();
  45.         conn.close();
  46.         return result;
  47.     }
  48.     public void StoreData( RecordItem item ) throws Exception {
  49.         Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
  50.         String url = "jdbc:microsoft:sqlserver://localhost:1433;" +
  51.             "DatabaseName=Manpower";
  52.         conn = DriverManager.getConnection( url, "sa", "" );
  53.         stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
  54.                                      ResultSet.CONCUR_UPDATABLE );
  55.         String SQL = "select * from WorkTime where EmployeeID = '" +
  56.             item.GetEmployeeID() + "'";
  57.         rs = stmt.executeQuery( SQL ); //查找是否存在该员工的记录
  58.         //如果已经存在该员工的记录则更新,如果不存在则插入新的记录
  59.         if ( rs.next() ) {
  60.             SQL = "update WorkTime set EmployeeName='" + item.GetEmployeeName() +
  61.                 "',"
  62.                 + " OnworkTime='" + item.GetOnWorkTime() + "',"
  63.                 + " OffworkTime='" + item.GetOffWorkTime() + "',"
  64.                 + " LeaveworkTime='" + item.GetLeaveWorkTime() + "',"
  65.                 + " Describe='" + item.GetDescribe() + "'"
  66.                 + " where EmployeeID = '" + item.GetEmployeeID() + "'";
  67.             stmt.execute( SQL );
  68.         }
  69.         else {
  70.             SQL =
  71.                 "insert WorkTime(EmployeeID,EmployeeName,OnworkTime, OffworkTime,LeaveworkTime,Describe) "
  72.                 + " values('" + item.GetEmployeeID() + "', '"
  73.                 +item.GetEmployeeName() + "','"
  74.                 + item.GetOnWorkTime() + "','"
  75.                 + item.GetDescribe() + "')";
  76.             stmt.execute( SQL );
  77.         }
  78.         rs.close();
  79.         stmt.close();
  80.         conn.close();
  81.     }
  82.     //删除相应工号的员工记录
  83.     public void DeleteData( String strEmployeeID ) throws Exception {
  84.         Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
  85.         String url = "jdbc:microsoft:sqlserver://localhost:1433;" +
  86.             "DatabaseName=Manpower";
  87.         conn = DriverManager.getConnection( url, "sa", "" );
  88.         stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
  89.                                      ResultSet.CONCUR_UPDATABLE );
  90.         String SQL = "delete from WorkTime where EmployeeID='" + strEmployeeID +
  91.             "'";
  92.         stmt.execute( SQL ); //查找是否存在该员工的记录
  93.     }
  94. }