我有两个带有外键的表,它们与它们相关:
enter image description here
我已经使用SQL查询从两个表中检索值:
User u = new User();
String sql = "SELECT * FROM user INNER JOIN account ON user.id = account.user WHERE user.id = 1 ";
try {
Statement stm = con.createStatement();
ResultSet rsu = stm.executeQuery(sql);
while(rsu.next()){
u.setFname(rsu.getString("fname"));
u.setLname(rsu.getString("lname"));
u.setMname(rsu.getString("mname"));
u.setGender(rsu.getString("gender"));
u.setAddress(rsu.getString("address"));
u.setCitizenship(rsu.getLong("citizenship"));
/**
*
* Here i want to get values of account table and set it on
* user object to return u
*
*/
}
return u;
} catch (SQLException e) {
e.printStackTrace();
}
最佳答案
很容易获得表的值。您需要做的就是提供您已经在ResultSet中获得的帐户表的列名。
u.setAccountID(rsu.getLong("accId"));
此处,帐户ID存储在结果集中的对象中,该对象已从结果集中的数据库中检索到