📄 usereo.java
字号:
package book.portal.table;
import java.sql.ResultSet;
import java.sql.SQLException;
import book.portal.DbManager;
public class UserEO {
protected int id;//代表数据库中user_id列
protected String userName;//代表数据库中user_name列
protected String password;//代表数据库中password列
protected String displayName;//代表数据库中display_name列
protected String emailAddress;//代表数据库中email_address列
protected String phoneNumber;//代表数据库中phone_number列
protected String activeStatus;//代表数据库中active_status列
public UserEO() {//无参的构造方法
this.id = -1;
}
public UserEO(int id) {//有参的构造方法,参数为user_id
this.id = id;
if (!FromDb())//如果没有找到该id的user
this.id = -1;
}
public boolean FromDb() {//从数据库中读出,并更新bean
int row = -1;
//读记录的sql语句
String sql = "select * from ajax_user where user_id=" + this.id
+ " and active_status='Y'";
ResultSet rs = DbManager.getResultSet(sql);//执行sql语句并返回ResultSet
try {
rs.last();//移动到最后一行
row = rs.getRow();//得到总记录数
if (row == 1) {//如果只查询到一条记录,则代表该记录存在并更新该类的属性
this.userName = rs.getString("USER_NAME");
this.password = rs.getString("PASSWORD");
this.displayName = rs.getString("DISPLAY_NAME");
this.emailAddress = rs.getString("EMAIL_ADDRESS");
this.phoneNumber = rs.getString("PHONE_NUMBER");
this.activeStatus = rs.getString("ACTIVE_STATUS");
return true;
} else
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {//最后关闭ResutltSet,Statement.并释放连接
try {
if (rs != null)
rs.close();
if (rs.getStatement() != null)
rs.getStatement().close();
DbManager.releaseConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public boolean ToDb() {//更新数据库,并重新设置bean
if (getId() == -1)//如果此时id为-1
{
return false;
} else {
//更新该记录的sql语句
String sql = "update ajax_user set user_name=" + getUserName()
+ ",password='" + getPassword()
+ "',display_name='" + getDisplayName()
+ "',email_address='" + getEmailAddress()
+ "',phone_number='" + getPhoneNumber()
+ "',active_status='"+ getActiveStatus()
+ "' where layout_id=" + getId();
DbManager.excute(sql);//执行sql语句
return FromDb();//重新读出bean的属性
}
}
public String getActiveStatus() {//获得activeStatus属性的方法
return activeStatus;
}
public void setActiveStatus(String activeStatus) {//设置activeStatus属性的方法
this.activeStatus = activeStatus;
}
public String getDisplayName() {//获得displayName属性的方法
return displayName;
}
public void setDisplayName(String displayName) {//设置displayName属性的方法
this.displayName = displayName;
}
public String getEmailAddress() {//获得emailAddress属性的方法
return emailAddress;
}
public void setEmailAddress(String emailAddress) {//设置emailAddress属性的方法
this.emailAddress = emailAddress;
}
public int getId() {//获得id属性的方法
return id;
}
public void setId(int id) {//设置id属性的方法
this.id = id;
}
public String getPassword() {//获得password属性的方法
return password;
}
public void setPassword(String password) {//设置password属性的方法
this.password = password;
}
public String getPhoneNumber() {//获得phoneNumber属性的方法
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {//设置phoneNumber属性的方法
this.phoneNumber = phoneNumber;
}
public String getUserName() {//获得userName属性的方法
return userName;
}
public void setUserName(String userName) {//设置userName属性的方法
this.userName = userName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -