我在发生SQLException时遇到问题,在代码中使用方法getText()和PreparedStatement将字符串传递给SQL查询。我了解代码中的某个地方有错误,但是我找不到它,因此请寻求帮助。预先感谢您的关注。 我的代码:
try (Connection con = DriverManager.getConnection(url, user, password)) {
String sql = "SELECT user_id, name, lastname, information FROM users
WHERE name = ?";
String Name = field1.getText();
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, Name);
ResultSet result = ps.executeQuery();
while (result.next()) {
int id = result.getInt(1);
String fname = result.getString(2);
String lname = result.getString(3);
String inf = result.getString(4);
System.out.println ( id + fname + lname + gen + inf );
con.close();
ps.close();
}
} catch (SQLException sqle) {
System.out.println ("SQLException");
}