Database.java~94~
上传用户: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 ].SetOffworkItem( rs.getString( "OffworkTime" ) );
  34.             item[ i ].SetEmployeeID( rs.getString( "LeaveworkTime" ) );
  35.             item[ i ].SetEmployeeID( 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.GetOnrWorkTime() + "',"
  63.                 + " Describe='" + item.GetDescribe() + "'"
  64.                 + " where EmployeeID = '" + item.GetEmployeeID() + "'";
  65.             stmt.execute( SQL );
  66.         }
  67.         else {
  68.             SQL =
  69.                 "insert WorkTime(EmployeeID,EmployeeName,OnworkTime, Describe) "
  70.                 + " values('" + item.GetEmployeeID() + "', '" +
  71.                 item.GetEmployeeName()
  72.                 + "','" + item.GetOnrWorkTime() + "','" + item.GetDescribe() +
  73.                 "')";
  74.             stmt.execute( SQL );
  75.         }
  76.         rs.close();
  77.         stmt.close();
  78.         conn.close();
  79.     }
  80.     //删除相应工号的员工记录
  81.     public void DeleteData( String strEmployeeID ) throws Exception {
  82.         Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
  83.         String url = "jdbc:microsoft:sqlserver://localhost:1433;" +
  84.             "DatabaseName=Manpower";
  85.         conn = DriverManager.getConnection( url, "sa", "" );
  86.         stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
  87.                                      ResultSet.CONCUR_UPDATABLE );
  88.         String SQL = "delete from WorkTime where EmployeeID='" + strEmployeeID +
  89.             "'";
  90.         stmt.execute( SQL ); //查找是否存在该员工的记录
  91.     }
  92. }