📄 customer.java
字号:
package mylib;
public class Customer {
/*通过userName从数据库读取匹配的记录,并返回到Customer实例的属性*/
public void load(String userName) throws Exception {
ConnectionDB condb = new ConnectionDB();
System.out.println("查找用户"+userName);
String sqlstr = "SELECT "+
"UserName,TrueName,Password,Age,Sex,City,Address,PostCode,CardNo,CardType,"+
"Telphone,Email,Grade,Amount,Freeze "+
"FROM tb_Customer where UserName = ?";
Object[] parameter = {userName};
java.sql.ResultSet rs = condb.executeQuery(sqlstr,parameter);
if(rs.next()) {
System.out.println("初始化用户...");
this.userName = (String)rs.getObject("UserName");
System.out.println("UserName: "+userName);
this.trueName = (String)rs.getObject("TrueName");
System.out.println("TrueName: "+trueName);
this.password = (String)rs.getObject("Password");
System.out.println("Password: "+password);
this.age = rs.getInt("Age");
System.out.println("Age: "+age);
this.sex = (String)rs.getObject("Sex");
System.out.println("Sex: "+sex);
this.city = (String)rs.getObject("City");
System.out.println("City: "+city);
this.address = (String)rs.getObject("Address");
System.out.println("Address: "+address);
this.postCode = (String)rs.getObject("PostCode");
System.out.println("PostCode: "+postCode);
this.cardNo = (String)rs.getObject("CardNo");
System.out.println("CardNo: "+cardNo);
this.cardType = (String)rs.getObject("CardType");
System.out.println("CardType: "+cardType);
this.telphone = (String)rs.getObject("Telphone");
System.out.println("Telphone: "+telphone);
this.email = (String)rs.getObject("Email");
System.out.println("Email: "+email);
this.grade = rs.getInt("Grade");
System.out.println("Grade: "+grade);
this.amount = rs.getDouble("Amount");
System.out.println("Amount: "+amount);
this.freeze = rs.getInt("Freeze");
System.out.println("Freeze: "+freeze);
System.out.println("初始化用户成功");
}
else{
System.out.println("找不到用户"+userName);
}
/*释放数据库资源*/
condb.close();
condb = null;
}
/******************************************************
*
*用户更新数据操作
*把用户信息保存到数据库
*不用指定列名,更新所有字段
*
******************************************************/
public void update() {
ConnectionDB condb = new ConnectionDB();
String sqlstr = "Update tb_Customer set TrueName=?,Password=?,Sex=?,City=?"+
",Address=?,PostCode=?,CardNo=?,CardType=?,Telphone=?,Email=? where UserName=?";
Object[] parameter = {this.trueName,this.password,this.sex,this.city,this.address
,this.postCode,this.cardNo,this.cardType,this.telphone,this.email
,this.userName};
int rows = condb.executeUpdate(sqlstr,parameter);
condb.close();
condb = null;
if(rows==1) {
System.out.println("用户"+userName.trim()+"更新成功!影响行数"+rows);
}
}
/*****************************************************
*
*获取该用户的属性
*
****************************************************/
public String getUserName() {
return this.userName;
}
public String getPassword() {
return this.password;
}
public String getTureName() {
return this.trueName;
}
public String getSex() {
return this.sex;
}
public String getCity() {
return city;
}
public String getAddress() {
return address;
}
public String getPostCode() {
return postCode;
}
public String getCardNo() {
return cardNo;
}
public String getCardType() {
return cardType;
}
public String getTelphone() {
return telphone;
}
public String getEmail() {
return email;
}
public int getGrade() {
return grade;
}
public double getAmount() {
return amount;
}
public int getFreeze() {
return freeze;
}
/*****************************************************
*
*设置该用户属性
****************************************************/
public void setPassword(String password) {
this.password = password;
}
public void setTureName(String trueName) {
this.trueName = trueName;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setCity(String city) {
this.city = city;
}
public void setAddress(String address) {
this.address = address;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public void setEmail(String email) {
this.email = email;
}
public void setGrade(int grade) {
this.grade = grade;
}
public void setAmount(double amount) {
this.amount = amount;
}
public void setFreeze(int freeze) {
this.freeze = freeze;
}
//用户基本属性
private String userName = null; //注册名 (20)
private String trueName = null; //真实姓名 (20)
private String password = null; //密码 (20)
private int age = 1; //年龄 (samllint 2)
private String sex = null; //性别 (2)
private String city = null; //城市 (20)
private String address = null; //地址 (50)
private String postCode = null; //邮编 (6)
private String cardNo = null; //证件号 (24)
private String cardType = null; //证件类型 (10)
private String telphone = null; //联系电话 (20)
private String email = null; //电子邮箱 (30)
//系统管理属性
private int grade = 0; //等级 (int 4)
private double amount = 0.0; //消费 (money 8)
private int freeze = 0; //冻结状态 (tinying 1)
/****************************************
*测试main()
*******************************************/
public static void main(String[] args) {
try {
Customer obj = new Customer();
obj.load("tom");
//obj.setPassword("123");
//obj.setAddress("天河区");
//obj.update();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -