📄 customerregistry.java
字号:
/**********************************************
*
*类CustomerRegistry的实例保存当前正在注册的用户
*信息,并访问数据库确定该用户未被注册,如果数据库中
*没有记录则保存当前用户信息到数据库
*
*
**********************************************/
package mylib;
public class CustomerRegistry {
public CustomerRegistry() {
}
/************************************
*用指定属性值初始化Customer实例
*************************************/
public void initialize(String userName,
String trueName,
String password,
int age,
String sex,
String city,
String address,
int postCode,
String cardNo,
String cardType,
String telphone,
String email) {
//-------------------------------------------
this.userName = userName;
this.trueName = trueName;
this.password = password;
this.age = new Integer(age);
this.sex = sex;
this.city = city;
this.address = address;
this.postCode = new Integer(postCode);
this.cardNo = cardNo;
this.cardType = cardType;
this.telphone = telphone;
this.email = email;
}
/***************************************
*持久化该用户的实例
***************************************/
public String store() {
ConnectionDB condb = new ConnectionDB();
/*验证用户名是否重复*/
String str = "Select * from tb_Customer where UserName = \'"+userName+"\'";
java.sql.ResultSet rs = condb.executeQuery(str);
String message = "non-message";
try {
if(rs.next()) {
System.out.println("用户"+userName+"已存在,请使用其它名字。");
message = "对不起,该名称已经被注册,请使用其它名称,谢谢.";
condb.close();
}
else {
String sql = "Insert into tb_Customer"+
"(UserName,TrueName,Password,Age,Sex,City,Address,PostCode,CardNo,CardType,Telphone,Email) "+
"Values(?,?,?,?,?,?,?,?,?,?,?,?)";
Object[] parameter = {
this.userName,
this.trueName,
this.password,
this.age,
this.sex,
this.city,
this.address,
this.postCode,
this.cardNo,
this.cardType,
this.telphone,
this.email,
};
int rows = condb.executeUpdate(sql,parameter);
System.out.println("用户 "+userName+"注册成功.."+"影响行数:"+rows);
if(rows==1)
message = "恭喜你,注册成功!";
else if(rows>1)
message = "插入了"+rows+"行";
else
message = "注册失败";
condb.close();
}
}
catch(Exception ex) {
System.out.println("sotre()");
ex.printStackTrace();
}
return message;
}
//用户基本属性
private String userName = null; //注册名 (20)
private String trueName = null; //真实姓名 (20)
private String password = null; //用户密码 (20)
private int age = 0; //年龄 (smallint 2)
private String sex = null; //性别 (2)
private String city = null; //城市 (20)
private String address = null; //地址 (50)
private int postCode = 0; //邮编 (6)
private String cardNo = null; //证件号 (24)
private String cardType = null; //证件类型 (10)
private String telphone = null; //联系电话 (20)
private String email = null; //电子邮箱 (30)
/**************************************************
*测试main()
*****************************************************/
public static void main(String[] args) {
try {
CustomerRegistry cr = new CustomerRegistry();
cr.initialize("chien4","笨7仔","123456",12,"男","广州","天河区大观路496号",563000,"111122222","学生证","8888888","ben7zi@qq.com");
System.out.println(cr.store());
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -