📄 user.java
字号:
//系统用户类
package Information;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import comm.*;
public class User {
private String UserId; // 用户名
private String EmplName; // 员工姓名
private String PassWord; // 密码
private int UserType; // 用户类型;1-经理;2- 管理员
StringOper so = new StringOper();
// 获得一个用户的信息,如果存在此用户返回true,否则false
public boolean getUser()throws Exception
{
int conditionNo = 0;
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql = "Select * from Users";
String condition = " where ";
// 设置查询条件
if(UserId != null){
condition += "UserId='"+UserId+"'";
conditionNo++;
}
if(PassWord != null)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " PassWord='"+PassWord+"'";
conditionNo++;
}
if(conditionNo > 0)
{
sql += condition;
}
System.out.println(sql);
try
{
// 执行查询操作
rs = o_DBOper.getResultSet(sql);
if(rs.next())
{
setUserId(so.ReplaceNull(rs.getString("UserId")));
setEmplName(so.ReplaceNull(rs.getString("EmplName")));
setPassWord(so.ReplaceNull(rs.getString("PassWord")));
setUserType(rs.getInt("UserType"));
return true;
}
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return false;
}
// 根据查询条件获得多个用户信息
public Vector getMoreUser() throws Exception
{
Vector v_User = new Vector();
int conditionNo = 0;
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql = "Select * from Users";
String condition = " where ";
try
{
if(UserId != null)
{
condition += " UserId='"+UserId+"'";
conditionNo++;
}
if(EmplName != null)
{
if(conditionNo > 0)
{
condition += " and ";
}
condition += " EmplName='"+EmplName+"'";
conditionNo++;
}
if(UserType != 0)
{
if(conditionNo > 0)
{
condition += " and ";
}
condition += " UserType="+UserType;
conditionNo++;
}
if(conditionNo > 0)
{
sql += condition;
}
sql += " order by UserId desc";
rs = o_DBOper.getResultSet(sql);
//System.out.println(sql);
while(rs.next())
{
User o_User2 = new User();
o_User2.setUserId(so.ReplaceNull(rs.getString("UserId")));
o_User2.setEmplName(so.ReplaceNull(rs.getString("EmplName")));
o_User2.setPassWord(so.ReplaceNull(rs.getString("PassWord")));
o_User2.setUserType(rs.getInt("UserType"));
v_User.add(o_User2);
}
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return v_User;
}
//删除用户信息,条件为指定用户名
public void DeleteUser(String UserId) throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql_dlt = "delete from Users where UserId ='" + UserId + "'";
try
{
o_DBOper.getResultSet(sql_dlt);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
//插入用户,密码缺省为6个1
public void CreateUser() throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql = "Insert into Users(UserId,EmplName,UserType,PassWord) ";
sql =sql+"values('"+UserId+"','"+EmplName+"',"+UserType+",'111111')";
//System.out.println("INSERT SQL:"+sql);
try
{
o_DBOper.getResultSet(sql);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 更新用户信息,用于管理员更改用户信息,不更改密码
public void UpdateUser() throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql = "Update Users set EmplName='"+EmplName+"',UserType="+UserType;
sql = sql+" where UserId='"+UserId+"'";
// System.out.println("UPDATE SQL :"+sql);
try
{
o_DBOper.getResultSet(sql);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 用户更改自己密码和管理员密码重置为六个1
public void UpdatePassWord() throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql = "Update Users set PassWord='"+PassWord+"' where UserId='"+UserId+"'";
try
{
o_DBOper.getResultSet(sql);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 设置、获得用户的信息
public void setUserId(String UserId){
this.UserId = UserId;
}
public String getUserId(){
return this.UserId;
}
public void setEmplName(String EmplName){
this.EmplName = EmplName;
}
public String getEmplName(){
return this.EmplName;
}
public void setPassWord(String PassWord){
this.PassWord = PassWord;
}
public String getPassWord(){
return this.PassWord;
}
public void setUserType(int UserType){
this.UserType = UserType;
}
public int getUserType(){
return this.UserType;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -