1. 程式人生 > 資料庫 >Java(Eclipse jdk1.7)+SQL Server(2008 r2)+JDBC4實現高校工資管理系統

Java(Eclipse jdk1.7)+SQL Server(2008 r2)+JDBC4實現高校工資管理系統

##高效工資管理系統主要是實現教職工、部門、工資資訊的管理
一、環境搭建
OS為WIN7,其餘使用的平臺及工具如下:

1.Eclipse jdk1.7

參考部落格:官網下載JDK1.7的方法和步驟()
注意:jdk的版本位數要與Eclipse的版本位數一致,我先前沒注意這點,直接下載的64位的jdk(因為我的電腦OS是64位的),安裝後報錯才發現我的Eclipse是32位的,與安裝的jdk位數不一致,後又解除安裝重新安裝的32位的jdk1.7

2.SQL Server 2008R2

SQL Server的安裝這裡不詳細介紹了,網上也有許多參考教程。不過有一個錯誤需要特別說明一下,本人在這折騰了好久

問題
無法將型別為“System.__ComObject”的 COM 物件強制轉換為介面型別“Microsoft.VisualStudio.OLE.Interop.IServiceProvider”。此操作失敗的原因是對 IID 為“{6D5140C1-7436-11CE-8034-00AA006009FA}”的介面的 COM 元件呼叫 QueryInterface 因以下錯誤而失敗: 不支援此介面 (異常來自 HRESULT:0x80004002 (E_NOINTERFACE))。 (Microsoft.VisualStudio.OLE.Interop)
解決:
錯誤原因應該和IE的ieproxy.dll檔案有關,看了網上的解答,重灌IE或許能解決,但我發現我解除安裝不了IE,最後從別人的電腦(同類型的機器)上拷貝了一份IE檔案替換之後,問題解決了。
參考部落格:
無法將型別為“System.__ComObject”的 COM 物件強制轉換為介面型別“Microsoft.VisualStudio.OLE.Interop.IServiceProvider”。()
由於上面的問題我把SQLServer重新安裝了好幾次,所以這裡也附上解除安裝SQL Server的參考部落格:解除安裝sqlserver2008,完全清除()

3.sqljdbc4的安裝

我安裝的版本是jdbc4,下載之後直接把jdbc放在了Eclipse的專案資料夾裡,之後在相應的資料夾下配置構建路徑(JRE系統庫右鍵選擇即可),在新增外部JAR(X)…裡開啟jdbc即將其新增至引用的庫裡面了。

參考部落格:配置eclipse通過JDBC連線SQl Server 2008R2資料庫()
java連線sql server 2008和連線sql server 2008(完整版):
最後可以進行相應的測試,我的測試程式碼如下:

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class demo {
   public static void main(String[] args) {
       String user = "hhh";
       String password = "hhhhhhhhhh";
       Connection conn;
       Statement stmt;
       ResultSet rs;
       String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Test;";
       String sql = "select * from Patient";
       try {
           // 連線資料庫
           conn = DriverManager.getConnection(url, user, password);
           // 建立Statement物件
           stmt = conn.createStatement();
           // 執行資料庫查詢語句
           rs = stmt.executeQuery(sql);
           while (rs.next()) {
               String id = rs.getString("PID");
               String name = rs.getString("PName");
              System.out.println("學號 "+id+"姓名 "+name);
           }
           if (rs != null) {
               rs.close();
               rs = null;
           }
           if (stmt != null) {
               stmt.close();
               stmt = null;
           }
           if (conn != null) {
               conn.close();
               conn = null;
           }
       } catch (SQLException e) {
           e.printStackTrace();
           System.out.println("資料庫連線失敗");
       }
   }
}

二、系統實現
本次實現的系統比較簡單,具體的系統ER圖如下所示:
學校工資管理系統ER圖
根據E-R圖向關係模型的轉換原則,學校的工資管理系統的實體和聯絡可以轉換為下列關係模式:

管理員(主碼:使用者號,密碼)
教職工(主碼:教工號,姓名,性別,年齡,教學背景,所屬部門,職稱,職務,外碼:所屬部門編號)
部門(主碼:部門編號,部門名稱,部門人數,部門負責人)
工資(主碼(外碼):教工號,基本工資,應加工資,應扣工資,應發工資,實發工資,工資日期,加扣標誌)

1、首先需要建立關係模式:

(1)教職工表 
create table Staff(--建立教職工表
Sno NCHAR(10) primary key NOT NULL,
Name VARCHAR(50) NOT NULL,
Gender CHAR(2)CHECK(Gender IN('男','女')),
Age INT NOT NULL,
Education VARCHAR(50),
Department VARCHAR(50) NOT NULL,
Zhicheng VARCHAR(50) not null,
Zhiwu VARCHAR(50) not null,
Dno NCHAR(10) foreign key references Depart(Dno) )
(2)部門表 
create table Depart(--建立部門表
Dno NCHAR(10) primary key NOT NULL,
Department VARCHAR(50) NOT NULL,
Num INT NOT NULL,
Header VARCHAR(50) NOT NULL)
(3)工資表 
CREATE TABLE Wage(--建立工資表
Sno nchar(10) not null primary key foreign key references Staff(Sno),
Basewage decimal,
Addwage decimal,
Reducewage decimal,
Shouldwage decimal,
Realwage decimal,
Datewage date,
Flag int )
alter table Wage add Taxwage decimal--修改工資表,新增一列個人所得稅
alter table Wage add constraint c1 check(Shouldwage=Basewage+Addwage-Reducewage);
alter table Wage add constraint c2 check(Flag in(0,1));
alter table Wage add constraint c3 check(Realwage between 0 and Shouldwage);
alter table Wage add constraint c4 check(Realwage=Basewage+Addwage-Reducewage-Taxwage);
(4)使用者登入表 
create table UserPwd(--建立使用者登入表
Sno NCHAR(10) primary key NOT NULL,
Pwd  NCHAR(10) NOT NULL)
(5)管理員登入表 
create table AdminPwd(--建立管理員登入表
Sno NCHAR(10) primary key NOT NULL,
Pwd  NCHAR(10) NOT NULL)

**建立儲存過程來統計某一個月應發的工資總和,這裡以2019年12月份為例**

use [SalarySystem] 	--建立儲存過程統計實發工資總和
go
create procedure Sum_Realwage
@Realsalary decimal output
as
select SUM(Realwage) as Sumrealwage from dbo.Wage where '2019-12-01'<=Datewage and Datewage<='2019-12-31'
go
declare @sum decimal--宣告輸出引數並執行儲存過程
exec Sum_Realwage @sum output
print @sum

**建立觸發器**
(1)工作表的插入、修改觸發器(當往教職工工資專案表中插入記錄或更新記錄時,自動修改該職工的應發工資數和實發工資數)
create trigger wage_insert_update
on Wage for insert,update
as begin
update Wage set Shouldwage=Basewage+Addwage-Reducewage,Realwage=Basewage+Addwage-Reducewage-Taxwage where Sno in(select Sno from inserted)
end;
(2)教職工插入、刪除觸發器(當向教職工表裡刪除或增加一個員工時,部門表對應的部門人數相應變化)
create trigger delete_staff --從教職工表Staff裡刪除教職工,部門表Depart裡的部門人數減1
on Staff after delete
as begin
update Depart set Num=Num-1 where Dno in(select Dno from deleted)
end;
create trigger insert_staff  --向教職工表裡增加教職工,部門表Depart裡的部門人數加1
on Staff after insert
as begin
update Depart set Num=Num+1 where Dno in(select Dno from inserted)
end;

2、程式碼實現

(1). 登入 (增加兩個JRadionButton按鈕物件rbt1、rtb2和一個標誌值flag,通過isSelected()方法判斷登入的使用者是管理員(令flag=1)還是教職工(flag=0),密碼驗證通過後根據flag值跳轉到不同的功能主介面)

public void isLogin(int flag)//判斷登入		{
           getDataFromDatabase database=new getDataFromDatabase(); //新建資料庫物件
           String LoginID =text1.getText();//text1為教工號輸入框;
           String password=new String(text2.getText());
           String pswdDatabase1=String.valueOf(database.getDatabySql("select Pwd from AdminPwd where Sno="+LoginID,"Pwd"));  //從資料庫匯出管理員密碼
           String pswdDatabase0=String.valueOf(database.getDatabySql("select Pwd from UserPwd where Sno="+LoginID,"Pwd"));  //從資料庫匯出教職工密碼
       	   if(flag==1&&password.equals(pswdDatabase1)){       //管理員登入
               setVisible(false);
               new MainWindow();//管理員的功能主介面
               dispose();
           }
       	   else if(flag==0&password.equals(pswdDatabase0)){     //教職工登入
   	            setVisible(false);
   	            new LimitedWindow();//教職工登入的主界
   	            dispose();
   	        }
           else if(LoginID.equals("0")&&password.equals("0")){      
               setVisible(false);
               dispose();
           }
           else {      //賬號或密碼錯誤,清空輸入框的內容
               JOptionPane.showMessageDialog(null,"使用者名稱或密碼錯誤","通知",JOptionPane.WARNING_MESSAGE);
               g1.clearSelection();//g1是一個按鈕組,包含了rbt1和rtb2,清空單擊按鈕 	            text1.setText(""); //清空輸入框
               text2.setText("");
           }
       }

(2) 資料庫連線查詢 (封裝資料庫的連線和查詢方法,傳入引數String sql是要執行的SQL語句、String item是從資料庫返回的要查詢的屬性值,返回型別是String,若查詢不到,返回的String為空值)

public String getDatabySql(String sql,String item){
        String url = "jdbc:sqlserver://localhost:1433;DatabaseName=SalarySystem;";
        try {
            conn = DriverManager.getConnection(url, "zcf", "zhuchengfei"); 連線資料庫
            stmt = conn.createStatement(); // 建立Statement物件傳送SQL語句
            rs = stmt.executeQuery(sql);   // 執行資料庫查詢語句
            while (rs.next()) {		//rs.next()返回布林值,且游標跳轉到下一行
                return (rs.getString(item).trim());//返回一個具體的屬性值
            }
            if (rs != null) { rs.close(); rs = null; }
            if (stmt != null) {stmt.close();stmt = null;}
            if (conn != null) { conn.close(); conn = null; }
            return "";
        }
        catch (SQLException e) {
            e.printStackTrace();System.out.println("資料庫連線失敗");
        }
        return "";//查詢不到,返回空
    }

三、參考部落格

【1】官網下載JDK1.7的方法和步驟
【2】無法將型別為“System.__ComObject”的 COM 物件強制轉換為介面型別“Microsoft.VisualStudio.OLE.Interop.IServiceProvider”
【3】解除安裝sqlserver2008,完全清除
【4】配置eclipse通過JDBC連線SQl Server 2008R2資料庫()
【5】java連線sql server 2008和連線sql server 2008(完整版):