我有两个表,即USR_TABLE和USR_PROFILE。
USR_TBALE有这些专栏:
在用户个人资料表中:
在用户表内部,我以编程方式插入userId(使用Java UUID),全名,电子邮件,user_status和密码。
我的问题是当我将ID插入USR_TABLE时,我是否必须在USR_PROFILE表中自动插入相同的ID以保持ID值相同?
如果是这样,我该怎么做呢?
现在我的代码是:
注册DAO:
public class RegistrationDAO {
public int insert(UserProfile user) {
int count = 0;
try {
String uniqueID = UUID.randomUUID().toString();
Connection con = DBConnection.getConnection();
String query = "insert into TBL_USER (USR_ID, USR_FULL_NAME,USR_PRIMARY_EMAIL,USR_PASSWORD,USR_STATUS) values(?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, uniqueID);
pst.setString(2, user.getFullName());
pst.setString(3, user.getEmail());
pst.setString(4, user.getPassword());
pst.setString(5, "PENDING");
count = pst.executeUpdate();
} catch (Exception e) {
System.out.println("@@@@Record insertion error in Registration DAO@@@@");
e.printStackTrace();
}
return count;
}
}
简介DAO:
public class ProfileDAO {
public void SaveProfile(UserProfile userProfile) {
Connection con = null;
try {
con = DBConnection.getConnection();
String query = "Insert into TBL_USER_PROFILE (USR_SUMMARY, USR_ROLE, USR_COMPANY, USR_FROM, USR_TO, USR_LOCATION) VALUES(?,?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, userProfile.getSummary());
pst.setString(2, userProfile.getRole());
pst.setString(3, userProfile.getCompany());
pst.setString(4, userProfile.getFrom());
pst.setString(5, userProfile.getTo());
pst.setString(6, userProfile.getLocation());
pst.executeUpdate();
} catch (Exception e) {
System.out.println("Error in Profile DAO class in method SaveProfile()");
e.printStackTrace();
}
}
}