📄 dbuser.java
字号:
size += CacheSizes.sizeOfString(passwordHash); //password
size += CacheSizes.sizeOfString(name); //name
size += CacheSizes.sizeOfString(email); //email
size += CacheSizes.sizeOfObject(); //property lock
size += CacheSizes.sizeOfProperties(properties);//properties object
return size;
}
//OTHER METHODS
/**
* Returns a String representation of the User object using the username.
*
* @return a String representation of the User object.
*/
public String toString() {
return username;
}
public int hashCode() {
return id;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof DbUser) {
return id == ((DbUser)object).getID();
}
else {
return false;
}
}
/**
* Loads user properties from the database.
*/
private void loadProperties() {
//If "anonymous" or "all users", do nothing.
if (id == -1 || id == 0) {
properties = new Properties();
return;
}
//Acquire a lock so that no other property loading or saving can be
//performed at the same time.
synchronized(propertyLock) {
Properties newProps = new Properties();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String s_userID = rs.getString(1);
String s_menuID = rs.getString(2);
newProps.put(s_userID, s_menuID);
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUser:loadProperties():" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
this.properties = newProps;
}
}
/**
* Loads user Menus from the database.
*/
private void loadMenus() {
//If "anonymous" or "all users", do nothing.
if (id == -1 || id == 0) {
menus = new String[0];
return;
}
//Acquire a lock so that no other property loading or saving can be
//performed at the same time.
synchronized(menusLock) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String s_menuID = rs.getString(2);
menus[menus.length] = s_menuID;
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUser:loadProperties():" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
/**
* Saves user properties to the database.
*/
private void saveProperties() {
//If "anonymous" or "all users", do nothing.
if (id == -1 || id == 0) {
return;
}
//Acquire a lock so that no other property loading or saving can be
//performed at the same time.
synchronized(propertyLock) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
//Delete all old values.
pstmt = con.prepareStatement(DELETE_PROPERTIES);
pstmt.setInt(1, id);
pstmt.execute();
pstmt.close();
//Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Enumeration enum = properties.keys();
while (enum.hasMoreElements()) {
enum.nextElement();
String s_menuID = (String) properties.get(id + "");
pstmt.setInt(1, id);
pstmt.setString(2, s_menuID);
pstmt.executeUpdate();
}
}
catch( SQLException sqle ) {
System.err.println(sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
/**
* Load the user data from the database.
*/
//tested OK
private void loadFromDb() throws UserNotFoundException {
//If the user is anonymous or "all users", do nothing.
if (id == -1 || id == 0) {
return;
}
// Otherwise, select user data from User table and fill in relevant fields.
String query;
//We may want to do a username lookup.
if (username != null) {
query = LOAD_USER_BY_USERNAME;
}
//Otherwise, a lookup by id
else {
query = LOAD_USER_BY_ID;
}
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(query);
if (username != null) {
pstmt.setString(1, username);
}
else {
pstmt.setInt(1, id);
}
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UserNotFoundException(
"Failed to read user " + id + " from database."
);
}
this.id = rs.getInt("userid");
this.username = rs.getString("user_id");
this.passwordHash = rs.getString("passwordhash");
this.name = rs.getString("myname");
this.email = rs.getString("email");
this.status = rs.getInt( "status");
}
catch( SQLException sqle ) {
throw new UserNotFoundException(
"Failed to read user " + id + " from database.", sqle
);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Inserts a new user record into the database.
*/
//tested OK
private void insertIntoDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_USER);
pstmt.setInt(1, id);
pstmt.setString(2, username);
pstmt.setString(3, passwordHash);
pstmt.setString(4, email);
pstmt.setString(5, name);
pstmt.setInt( 6, status );
// System.out.println( pstmt.toString());
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbUser:insertIntoDb()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Save the user data to the database.
*/
private void saveToDb() {
if ( id == -1 || id == 0 ) {
//"anonymous" or "all users", do nothing
return;
}
Connection con = null;
PreparedStatement pstmt = null;
//sj<
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_USER);
pstmt.setString(1, passwordHash);
pstmt.setString(2, email);
pstmt.setString(3, name);
pstmt.setInt(4, status );
pstmt.setString(5, username);
pstmt.setInt(6, id);
pstmt.executeUpdate();
}
//>sj
catch( SQLException sqle ) {
System.err.println( "SQLException in DbUser.java:saveToDb(): " + sqle );
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
public void setStatue(int st)throws UnauthorizedException
{
status = st;
saveToDb();
}
public void setUserName(String userName) throws UnauthorizedException {
username = userName;
saveToDb();
}
/**
* return the all of the permissions that the group does not have
*/
/*
public static void main (String[] args){
System.out.println("hello");
try {
DbUser user = new DbUser( new String("user1") );
System.out.println("user found:"+ user.toString()+user.email);
}
catch ( Exception e )
{
System.out.println("User not found");
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -