📄 user.java
字号:
package com.chapter15;
import java.util.*;
import java.sql.*;
import java.text.*;
public class User extends ExecuteDB
{
//定义类成员变量
private long UserID;
private String UserName;
private String UserPassword;
private String RealName;
private long DepartmentID;
private String Sex;
private String Degree;
private String BornDate;
private String UserSN;
private String Telephone;
private String Mobile;
private String Email;
private String InDate;
private String Intro;
private String Role;
private String strSql;
//初始化类成员变量
public User()
{
this.UserID=0;
this.UserName="";
this.UserPassword="";
this.RealName="";
this.DepartmentID=0;
this.Sex="";
this.Degree="";
this.BornDate = "";
this.UserSN="";
this.Telephone="";
this.Mobile="";
this.Email="";
this.InDate= "";
this.Intro="";
this.Role="0";
this.strSql="";
}
//向users数据表中添加一条新记录
public boolean add()
{
this.strSql="insert into users ";
this.strSql=this.strSql + "(";
this.strSql=this.strSql + "UserName,";
this.strSql=this.strSql + "UserPassword,";
this.strSql=this.strSql + "RealName,";
this.strSql=this.strSql + "DepartmentID,";
this.strSql=this.strSql + "Sex,";
this.strSql=this.strSql + "Degree,";
this.strSql=this.strSql + "BornDate,";
this.strSql=this.strSql + "UserSN,";
this.strSql=this.strSql + "Telephone,";
this.strSql=this.strSql + "Mobile,";
this.strSql=this.strSql + "Email,";
this.strSql=this.strSql + "InDate,";
this.strSql=this.strSql + "Intro,";
this.strSql=this.strSql + "Role";
this.strSql=this.strSql + ") ";
this.strSql=this.strSql + "values(";
this.strSql=this.strSql + "'" + this.UserName + "',";
this.strSql=this.strSql + "'" + this.UserPassword + "',";
this.strSql=this.strSql + "'" + this.RealName + "',";
this.strSql=this.strSql + "'" + this.DepartmentID + "',";
this.strSql=this.strSql + "'" + this.Sex + "',";
this.strSql=this.strSql + "'" + this.Degree + "',";
this.strSql=this.strSql + "'" + this.BornDate + "',";
this.strSql=this.strSql + "'" + this.UserSN + "',";
this.strSql=this.strSql + "'" + this.Telephone + "',";
this.strSql=this.strSql + "'" + this.Mobile + "',";
this.strSql=this.strSql + "'" + this.Email + "',";
this.strSql=this.strSql + "'" + this.InDate + "',";
this.strSql=this.strSql + "'" + this.Intro + "',";
this.strSql=this.strSql + "'" + this.Role + "'";
this.strSql=this.strSql + ")";
boolean isAdd = super.exeSql(this.strSql);
return isAdd;
}
//修改UserID对应的用户的信息
public boolean modify_info()
{
this.strSql="update users set";
this.strSql=this.strSql + " RealName=" + "'" + this.RealName + "',";
this.strSql=this.strSql + " DepartmentID=" + "'" + this.DepartmentID + "',";
this.strSql=this.strSql + " Sex=" + "'" + this.Sex + "',";
this.strSql=this.strSql + " Degree=" + "'" + this.Degree + "',";
this.strSql=this.strSql + " BornDate=" + "'" + this.BornDate + "',";
this.strSql=this.strSql + " UserSN=" + "'" + this.UserSN + "',";
this.strSql=this.strSql + " Telephone=" + "'" + this.Telephone + "',";
this.strSql=this.strSql + " Mobile=" + "'" + this.Mobile + "',";
this.strSql=this.strSql + " Email=" + "'" + this.Email + "',";
this.strSql=this.strSql + " InDate=" + "'" + this.InDate + "',";
this.strSql=this.strSql + " Intro=" + "'" + this.Intro + "'";
this.strSql=this.strSql + " where UserID='" + this.UserID + "'";
boolean isUpdate = super.exeSql(this.strSql);
return isUpdate;
}
//修改UserID对应的用户的密码
public boolean modify_UserPassword()
{
this.strSql="update users set ";
this.strSql=this.strSql + "UserPassword=" + "'" + this.UserPassword + "'";
this.strSql=this.strSql + " where UserID='" + this.UserID + "'";
boolean isUpdatekey = super.exeSql(this.strSql);
return isUpdatekey;
}
//验证用户名和密码是否正确
public boolean valid()
{
this.strSql="select UserID,UserName,Role from `users` ";
this.strSql=this.strSql + " where UserName='" + this.UserName + "'";
this.strSql=this.strSql + " and UserPassword='"+ this.UserPassword + "'";
try
{
ResultSet rs = super.exeQuery(this.strSql);
if (rs.next())
{
this.UserID=rs.getLong("UserID");
this.UserName=rs.getString("UserName");
this.Role = rs.getString("Role");
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
}
//判断用户名是否存在
public boolean exist()
{
this.strSql="select * from users ";
this.strSql=this.strSql + " where UserName='" + this.UserName + "'";
try
{
ResultSet rs = super.exeQuery(this.strSql);
if (rs.next())
{
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
}
//获取类成员变量UserID对应的用户信息
public boolean init()
{
this.strSql="select * from users where UserID=";
this.strSql=this.strSql + this.UserID;
try
{
ResultSet rs = super.exeQuery(this.strSql);
if (rs.next())
{
this.UserID=rs.getLong("UserID");
this.UserName=rs.getString("UserName");
this.UserPassword=rs.getString("UserPassword");
this.RealName=rs.getString("RealName");
this.DepartmentID=rs.getLong("DepartmentID");
this.Sex=rs.getString("Sex");
this.Degree=rs.getString("Degree");
this.BornDate=rs.getString("BornDate");
this.UserSN=rs.getString("UserSN");
this.Telephone=rs.getString("Telephone");
this.Mobile=rs.getString("Mobile");
this.Email=rs.getString("Email");
this.InDate=rs.getString("InDate");
this.Intro=rs.getString("Intro");
this.Role=rs.getString("Role");
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
return false;
}
}
//获取类UserName对应的用户信息
public boolean init_byUserName()
{
this.strSql="select * from users where UserName like ";
this.strSql=this.strSql + "'" + this.UserName + "'";
try
{
ResultSet rs = super.exeQuery(this.strSql);
if (rs.next())
{
this.UserID=rs.getLong("UserID");
this.UserName=rs.getString("UserName");
this.UserPassword=rs.getString("UserPassword");
this.RealName=rs.getString("RealName");
this.DepartmentID=rs.getLong("DepartmentID");
this.Sex=rs.getString("Sex");
this.Degree=rs.getString("Degree");
this.BornDate=rs.getString("BornDate");
this.UserSN=rs.getString("UserSN");
this.Telephone=rs.getString("Telephone");
this.Mobile=rs.getString("Mobile");
this.Email=rs.getString("Email");
this.InDate=rs.getString("InDate");
this.Intro=rs.getString("Intro");
this.Role=rs.getString("Role");
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
return false;
}
}
//获取成员变量DepartmentID对应的部门的所有人员信息,返回一个ResultSet类型对象
public ResultSet show_department_users()
{
this.strSql="select * from users where DepartmentID ="+this.DepartmentID;
ResultSet rs = null;
try
{
rs = super.exeQuery(this.strSql);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return rs;
}
//获取所有人员信息,返回一个ResultSet类型对象
public ResultSet show_users()
{
this.strSql="select UserID,UserName,RealName,DepartmentID,UserSN from users where Role = 0 order by UserSN desc";
ResultSet rs = null;
try
{
rs = super.exeQuery(this.strSql);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return rs;
}
//删除sUserID中对应的员工信息
public boolean delete(String sUserID)
{
this.strSql="delete from users where UserID in (";
this.strSql=this.strSql + sUserID + ")";
boolean isDelete = super.exeSql(this.strSql);
return isDelete;
}
//获取成员变量DepartmentID对应的部门的人员数量
public int count_department_users()
{
int iCount = 0;
this.strSql="select count(*) as num from users where DepartmentID ="+this.DepartmentID;
ResultSet rs = null;
try
{
rs = super.exeQuery(this.strSql);
if(rs.next())
iCount = rs.getInt("num");
}
catch(Exception e)
{
System.out.println(e.toString());
}
return iCount;
}
//设置类成员变量UserID的值
public void setUserID(long UserID)
{
this.UserID = UserID;
}
//获取类成员变量UserID的值
public long getUserID()
{
return this.UserID;
}
//设置类成员变量UserName的值
public void setUserName(String UserName)
{
this.UserName = UserName;
}
//获取类成员变量UserName的值
public String getUserName()
{
return this.UserName;
}
//设置类成员变量UserPassword的值
public void setUserPassword(String UserPassword)
{
this.UserPassword = UserPassword;
}
//获取类成员变量UserPassword的值
public String getUserPassword()
{
return this.UserPassword;
}
//设置类成员变量RealName的值
public void setRealName(String RealName)
{
this.RealName = RealName;
}
//获取类成员变量RealName的值
public String getRealName()
{
return this.RealName;
}
//设置类成员变量DepartmentID的值
public void setDepartmentID(long DepartmentID)
{
this.DepartmentID = DepartmentID;
}
//获取类成员变量DepartmentID的值
public long getDepartmentID()
{
return this.DepartmentID;
}
//设置类成员变量Sex的值
public void setSex(String Sex)
{
this.Sex = Sex;
}
//获取类成员变量Sex的值
public String getSex()
{
return this.Sex;
}
//设置类成员变量Degree的值
public void setDegree(String Degree)
{
this.Degree = Degree;
}
//获取类成员变量Degree的值
public String getDegree()
{
return this.Degree;
}
//设置类成员变量BornDate的值
public void setBornDate(String BornDate)
{
this.BornDate = BornDate;
}
//获取类成员变量BornDate的值
public String getBornDate()
{
return this.BornDate;
}
//设置类成员变量UserSN的值
public void setUserSN(String UserSN)
{
this.UserSN = UserSN;
}
//获取类成员变量UserSN的值
public String getUserSN()
{
return this.UserSN;
}
//设置类成员变量Telephone的值
public void setTelephone(String Telephone)
{
this.Telephone = Telephone;
}
//获取类成员变量Telephone的值
public String getTelephone()
{
return this.Telephone;
}
//设置类成员变量Mobile的值
public void setMobile(String Mobile)
{
this.Mobile = Mobile;
}
//获取类成员变量Mobile的值
public String getMobile()
{
return this.Mobile;
}
//设置类成员变量InDate的值
public void setInDate(String InDate)
{
this.InDate = InDate;
}
//获取类成员变量InDate的值
public String getInDate()
{
return this.InDate;
}
//设置类成员变量Intro的值
public void setIntro(String Intro)
{
this.Intro = Intro;
}
//获取类成员变量Intro的值
public String getIntro()
{
return this.Intro;
}
//设置类成员变量Email的值
public void setEmail(String Email)
{
this.Email = Email;
}
//获取类成员变量Email的值
public String getEmail()
{
return this.Email;
}
//设置类成员变量Role的值
public void setRole(String Role)
{
this.Role = Role;
}
//获取类成员变量Role的值
public String getRole()
{
return this.Role;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -