private UUID getClientID(String username) {
try {
String query = "SELECT id FROM `client_table` WHERE username=" + username;
stmt = connection.prepareStatement(query);
ResultSet rs = stmt.getResultSet();
return UUID.fromString(rs.getString(2));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
表格:
id username password create_date
id = c235a95d-58e7-4454-90ea-58396a00a3c5 username = myUsername 密码= [BLOB - 16 B] create_date = Fri Apr 28 09:34:49 BST 2017
那为什么会返回null?我认为我对ResultSet
做错了。
答案 0 :(得分:5)
字符串应该在两个'username'
之间,您已经使用PreparedStatement
,所以请使用:
String query = "SELECT id FROM `client_table` WHERE username= ?";
stmt = connection.prepareStatement(query);
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
return UUID.fromString(rs.getString(1));
}else{
return null;//id not found
}
注意强>
rs.getString(2)
没有第二个字段是错误的rs.next()
获取结果