package BeanPackage;
import java.sql.*;
public class Staff {
String name,sex,department,profession; //员工基本信息
int id,age;
Connection con;
Statement sql;
ResultSet rs;
public Staff()
{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //用纯java方式加载数据库
}
catch(Exception e){
System.out.println("加载数据库失败。 错误信息:"+e); //抛出异常信息
}
}
public void getMessage(int id)
{
try{
String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=factory"; //数据库的链接
con=DriverManager.getConnection(uri,"Staff","54321"); //员工用户登陆数据库
sql=con.createStatement(); //创建对象
rs=sql.executeQuery("SELECT * FROM Staff WHERE ID=this.id"); //查询该员工的所有基本信息
}
catch(Exception e){
System.out.println("链接数据库失败。 错误信息:"+e);
}
}
public void setId() //设置id,id项员工不可修改
{
try {
id=rs.getInt(1);
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setName(String name)
{
try {
if(this.name.equals(rs.getString(2))){ //判断名字是否被修改,被修改则进而修改数据库中对应的项目
name=this.name;
}
else {
int m=sql.executeUpdate("UPDATE Staff SET Name='"+this.name+"' WHERE ID=id");
}
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setSex(String sex)
{
try {
if(this.sex.equals(rs.getString(3))){
sex=this.sex;
}
else {
int m=sql.executeUpdate("UPDATE Staff SET Sex='"+this.sex+"' WHERE ID=id");
}
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setDepartment(String department)
{
try {
if(this.department.equals(rs.getString(5))){
department=this.department;
}
else {
int m=sql.executeUpdate("UPDATE Staff SET Department='"+this.department+"' WHERE ID=id");
}
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setProfession(String profession)
{
try {
if(this.profession.equals(rs.getString(6))){
profession=this.profession;
}
else {
int m=sql.executeUpdate("UPDATE Staff SET Profession='"+this.profession+"' WHERE ID=id");
}
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setAge(int age)
{
try {
if(this.age==rs.getInt(4)){
age=this.age;
}
else {
int m=sql.executeUpdate("UPDATE Staff SET Age='"+this.age+"' WHERE ID=id");
}
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public String getSex()
{
return sex;
}
public String getDepartment()
{
return department;
}
public int getAge()
{
return age;
}
public String getProfession()
{
return profession;
}
int month,absence;
float salary,basesalary,premium;
ResultSet ra,re;
public void setMonth(int month)
{
month=this.month;
int n=month+2;
try {
ra=sql.executeQuery("SELECT * FROM Attendance WHERE Id=id");
absence=ra.getInt(n);
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setSalary()
{
try
{
re=sql.executeQuery("SELECT * FROM Salary WHERE Id=id");
if(absence==0)
{
sql.executeUpdate("UPDATE Salary SET NoAbsence=200 WHERE ID=id");
salary=re.getFloat(3)+re.getFloat(4)+200;
}
else
{
salary=re.getFloat(3)+re.getFloat(4)-absence*50;
}
sql.executeUpdate("UPDATE Salary SET Salary='"+salary+"' WHERE ID=id");
}
catch (SQLException e)
{
System.out.println("错误信息:"+e);
}
}
public void setBasesalary()
{
try {
basesalary=re.getFloat(3);
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public void setPremium()
{
try {
premium=re.getFloat(4);
}
catch (SQLException e) {
System.out.println("错误信息:"+e);
}
}
public float getBasesalary()
{
return basesalary;
}
public float getpremium()
{
return premium;
}
public float getSalary()
{
return salary;
}
}