📄 user.java
字号:
package com.everstar.usermanage;
import java.sql.*;
import java.io.*;
import com.everstar.database.*;
public class User {
private int userID;
private String userName;
private String trueName;
private String password;
private String email;
private String Telephone;
private String Address;
private UserList userlist;
private Database dbConn;
// private static final String INSERT_JIVEUSER = "insert into JIVEUSER(userid,username,name,passwordhash,email,emailvisible,namevisible,isactor,isanonymous,actorid,valid)"
// +" values(?,?,?,?,?,1,1,0,0,0,1)";
// private static final String UPDATE_JIVEUSER = "update JIVEUSER set username=?,name=?,passwordhash=?,email=? where userid=?";
// private static final String LOAD_JIVEUSER_BY_ID = "select userid,username,name,passwordhash,email from JIVEUSER where userid=?";
// private static final String LOAD_JIVEUSER_BY_NAME = "select userid,username,passwordhash,email from JIVEUSER where username=?";
// private static final String SELECT_MAXID = "select max(userid) from JIVEUSER";
// private static final String GET_ACTOR = "select actorid from JIVEUSER where userid=? and isactor=0";
// private static final String SET_ACTOR = "update JIVEUSER set actorid=? where userid=? and isactor=0";
// private static final String IS_ACTOR = "select isactor where userid=?";
// private static final String GRANT_ACTOR = "update JIVEUSER set isactor=1 where userid=?";
// private static final String REVOKE_ACTOR = "update JIVEUSER set isactor=0 where userid=?";
// private static final String IS_LOCKED = "select valid from JIVEUSER where userid=?";
// private static final String LOCK = "update JIVEUSER set valid=0 where userid=?";
// private static final String UNLOCK = "update JIVEUSER set valid=1 where userid=?";
public User(String userName,String trueName,String password,String email,String Telephone,String Address, UserList userlist,Database dbConn)
{
this.dbConn = dbConn;
this.userID = getNextUserID();
this.trueName = trueName;
this.userName = userName;
this.password = StringUtils.hash(password);
this.Telephone = Telephone;
this.Address = Address;
this.userlist = userlist;
this.email = email;
insertIntoDb();
}
public User(int userID, UserList userlist,Database dbConn)
{
this.dbConn = dbConn;
this.userlist = userlist;
loadFromDb(userID);
}
public User(String userName, UserList userlist,Database dbConn)
{
this.dbConn = dbConn;
this.userlist = userlist;
loadFromDb(userName);
}
public int getUserID()
{
return userID;
}
public String getUserName()
{
return userName;
}
public String getPassword()
{
return password;
}
public String getEmail()
{
return email;
}
public void setUserID(int userID)
{
this.userID = userID;
updateDb();
}
public void setUserName(String name)
{
this.userName = name;
updateDb();
}
public void setPassword(String password)
{
this.password = StringUtils.hash(password);
updateDb();
}
public void setEmail(String email)
{
this.email = email;
updateDb();
}
public void settrueName(String trueName)
{
this.trueName = trueName;
updateDb();
}
public String gettrueName()
{
return trueName;
}
private int getNextUserID()
{
String SELECT_MAXID = "select max(userid) from JIVEUSER";
int temp = 0;
try
{
ResultSet rs =dbConn.select(SELECT_MAXID);
if (rs.next())
temp = rs.getInt(1);
rs.close();
dbConn.close();
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
return temp+1;
}
//select actorid from JIVEUSER where userid=? and isactor=0
public int getActor()
{
String GET_ACTOR = "select actorid from JIVEUSER where userid="+userID+" and isactor=0";
int actorid=0;
try
{
ResultSet rs =dbConn.select(GET_ACTOR);
if (rs.next())
actorid = rs.getInt(1);
rs.close();
dbConn.close();
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
return actorid;
}
//SET_ACTOR = "update JIVEUSER set actorid=? where userid=? and isactor=0";
public void setActor(int actorID)
{
String SET_ACTOR = "update JIVEUSER set actorid="+actorID+" where userid="+userID+" and isactor=0";
try
{
int i = dbConn.update(SET_ACTOR);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//private static final String IS_ACTOR = "select isactor where userid=?";
public boolean isActor()
{
String IS_ACTOR = "select isactor where userid="+userID;
int isactor=0;
try
{
ResultSet rs = dbConn.select(IS_ACTOR);
if (rs.next())
isactor = rs.getInt(1);
rs.close();
dbConn.close();
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
return (isactor==1)?true:false;
}
//private static final String GRANT_ACTOR = "update JIVEUSER set isactor=1 where userid=?";
public void grantActor()
{
String GRANT_ACTOR = "update JIVEUSER set isactor=1 where userid="+userID;
try
{
int i = dbConn.update(GRANT_ACTOR);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//private static final String REVOKE_ACTOR = "update JIVEUSER set isactor=0 where userid=?";
public void revokeActor()
{
String REVOKE_ACTOR = "update JIVEUSER set isactor=0 where userid="+userID;
try
{
int i = dbConn.update(REVOKE_ACTOR);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//private static final String IS_LOCKED = "select valid from JIVEUSER where userid=?";
public boolean isLocked()
{
int islocked=0;
String IS_LOCKED = "select valid from JIVEUSER where userid="+userID;
try
{
ResultSet rs = dbConn.select(IS_LOCKED);
if(rs.next())
islocked = rs.getInt(1);
rs.close();
dbConn.close();
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
return (islocked==0)?true:false;
}
//private static final String LOCK = "update JIVEUSER set valid=0 where userid=?";
public void lockUser()
{
String LOCK = "update JIVEUSER set valid=0 where userid="+userID+"";
try
{
int i = dbConn.update(LOCK);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//private static final String UNLOCK = "update JIVEUSER set valid=1 where userid=?";
public void unLockUser()
{
String UNLOCK = "update JIVEUSER set valid=1 where userid="+userID;
try
{
int i = dbConn.update(UNLOCK);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
private void insertIntoDb()
{
try
{
String INSERT_JIVEUSER = "insert into JIVEUSER(userid,username,name,passwordhash,email,emailvisible,namevisible,isactor,isanonymous,actorid,valid,telephone,address)"
+" values("+userID+",'"+userName+"','"+trueName+"','"+password+"','"+email+"',1,1,0,0,0,1,'"+Telephone+"','"+Address+ "')";
// String INSERT_JIVEUSER = "insert into JIVEUSER(userid,username,name,passwordhash,email,emailvisible,namevisible,isactor,isanonymous,actorid,valid)"
// +" values("+userID+",'"+userName+"','"+trueName+"','"+password+"','"+email+"',1,1,0,0,0,1)";
int i=dbConn.insert(INSERT_JIVEUSER);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//update database
private void updateDb()
{
try
{
String UPDATE_JIVEUSER = "update JIVEUSER set "
+"username='"+userName+"',name='"+userName+"',passwordhash='"+password+"',email='"+email
+"' where userid="+userID;
dbConn.update(UPDATE_JIVEUSER);
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//load user by userid
private void loadFromDb(int ID)
{
try
{
String LOAD_JIVEUSER_BY_ID = "select userid,username,name,passwordhash,email"+
" from JIVEUSER where userid="+ID;
ResultSet rs = dbConn.select(LOAD_JIVEUSER_BY_ID);
if (rs.next())
{
this.userID = rs.getInt("userid");
this.userName = rs.getString("username");
this.trueName = rs.getString("name");
this.password = rs.getString("passwordhash");
this.email = rs.getString("email");
}
rs.close();
dbConn.close();
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
//load user by username
private void loadFromDb(String name)
{
String LOAD_JIVEUSER_BY_NAME = "select userid,username,passwordhash,email"
+" from JIVEUSER where username='"+name+"'";
ResultSet rs = null;
try
{
rs=dbConn.select(LOAD_JIVEUSER_BY_NAME);
if (rs.next())
{
this.userID = rs.getInt(1);
this.userName = rs.getString(2);
this.trueName = rs.getString(3);
this.password = rs.getString(4);
this.email = rs.getString(5);
}
rs.close();
dbConn.close();
}
catch( Exception sqle )
{
sqle.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -