📄 dbuser.java
字号:
return username;
}
public int hashCode() {
return (int)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 properties from the database.
*/
private synchronized void loadPropertiesFromDb() {
this.properties = new Hashtable();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
properties.put(rs.getString(1), rs.getString(2));
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Saves properties to the database.
*/
private synchronized void savePropertiesToDb() {
boolean abortTransaction = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getTransactionConnection();
// Delete all old values.
pstmt = con.prepareStatement(DELETE_PROPERTIES);
pstmt.setLong(1, id);
pstmt.execute();
pstmt.close();
// Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
String value = (String)properties.get(name);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
abortTransaction = true;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
ConnectionManager.closeTransactionConnection(con, abortTransaction);
}
}
/**
* Deletes a property from the db.
*/
private synchronized void deletePropertyFromDb(String name) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_PROPERTY);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.execute();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Load the user data from the database.
*/
private void loadFromDb() throws UserNotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_USER_BY_ID);
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UserNotFoundException(
"Failed to read user with ID " + id + " from database."
);
}
this.originalUser = factory.getUserManager().getOriginalUser(id);
this.username = originalUser.getName();
this.name = originalUser.getRealName();
this.nameVisible = (rs.getInt(2) == 1);
this.email = originalUser.getEmail();
this.emailVisible = (rs.getInt(3) == 1);
this.rewardPoints = rs.getInt(4);
this.rate = rs.getInt(5);
this.coins = rs.getInt(6);
this.article = rs.getInt(7);
// We trim() the dates before trying to parse them because some
// databases pad with extra characters when returning the data.
this.creationDate =
originalUser.getRegTime();
// Now, load any extended message properties
if (!LAZY_PROP_LOADING) {
pstmt.close();
properties = new Hashtable();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();
while(rs.next()) {
properties.put(rs.getString(1), rs.getString(2));
}
}
}
catch( SQLException sqle ) {
throw new UserNotFoundException(
"Failed to read user " + id + " from database.", sqle
);
}
catch (NumberFormatException nfe) {
System.err.println("WARNING: There was an error parsing the dates " +
"returned from the database. Ensure that they're being stored " +
"correctly.");
throw new UserNotFoundException("User with id "
+ id + " could not be loaded from the database."
);
}
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.
*/
private void insertIntoDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_USER);
pstmt.setLong(1, id);
pstmt.setInt(2, nameVisible?1:0);
pstmt.setInt(3, emailVisible?1:0);
pstmt.setInt(4, rewardPoints);
pstmt.setInt(5, rate);
pstmt.setInt(6, coins);
pstmt.setInt(7, article);
pstmt.executeUpdate();
// Now, insert properties if there are any.
if (properties.size() > 0) {
// Close the previously opened statement.
pstmt.close();
// Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
String value = (String)properties.get(name);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
}
}
catch( SQLException 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() {
Connection con = null;
try {
con = ConnectionManager.getConnection();
saveToDb(con);
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Implementation of saveToDb that takes a Connection. This is useful for
* participating in transactions.
*/
private void saveToDb(Connection con) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(SAVE_USER);
if(password != null)
originalUser.setPassword(password);
if(name != null)
originalUser.setRealName(name);
pstmt.setInt(1, nameVisible?1:0);
if(email != null)
originalUser.setEmail(email);
pstmt.setInt(2, emailVisible?1:0);
pstmt.setInt(3, rewardPoints);
pstmt.setInt(4, rate);
pstmt.setInt(5, coins);
pstmt.setInt(6, article);
pstmt.setLong(7, id);
pstmt.executeUpdate();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#getArticleCount()
*/
public int getArticleCount() {
// TODO Auto-generated method stub
return article;
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#getId()
*/
public long getId() {
// TODO Auto-generated method stub
return id;
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#getOriginalUser()
*/
public com.airinbox.component.authorize.User getOriginalUser() {
// TODO Auto-generated method stub
return originalUser;
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#getUserCoins()
*/
public int getUserCoins() {
// TODO Auto-generated method stub
return coins;
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#getUserRate()
*/
public int getUserRate() {
// TODO Auto-generated method stub
return rate;
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#setArticleCount(int)
*/
public void setArticleCount(int count) throws SQLException {
// TODO Auto-generated method stub
int oldArticle = this.article;
this.article = count;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_ARTICLE);
pstmt.setInt(1, article);
pstmt.setLong(2, this.id);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
// Switch back to old point value.
this.article = oldArticle;
// Throw an exception so that the caller knows that the update
// operation failed.
throw sqle;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#setUserCoins(int)
*/
public void setUserCoins(int coin) throws SQLException {
// TODO Auto-generated method stub
int oldCoins = this.coins;
this.coins = coin;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_ARTICLE);
pstmt.setInt(1, coins);
pstmt.setLong(2, this.id);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
// Switch back to old point value.
this.coins = oldCoins;
// Throw an exception so that the caller knows that the update
// operation failed.
throw sqle;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/* (non-Javadoc)
* @see com.airinbox.member.forum.User#setUserRate(int)
*/
public void setUserRate(int rate) throws SQLException {
// TODO Auto-generated method stub
int oldRate = this.rate;
this.rate = rate;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_ARTICLE);
pstmt.setInt(1, rate);
pstmt.setLong(2, this.id);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
// Switch back to old point value.
this.rate = oldRate;
// Throw an exception so that the caller knows that the update
// operation failed.
throw sqle;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -