我在尝试将查询请求实现到程序中时遇到了此问题。没有这种方法连接到数据库工作正常,但是,这会抛出SQLException
。
String query = "SELECT EXISTS ( SELECT * FROM usr WHERE username = admin AND password = pass )";
boolean result;
Connection con = null;
try {
con = DriverManager.getConnection(url, username, password);
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery(query);
result = rs.next();
} catch (SQLException e) {
throw new RuntimeException("Cannot connect to the database!", e);
} finally {
System.out.println("Closing the connection.");
if (con != null)
try {
con.close();
} catch (SQLException ignore) {
System.out.println("Unknown Error Connecting to MySQL Server!");
}
}
我将查询带入了隔离它的方法。 任何帮助将不胜感激。
答案 0 :(得分:2)
通常搜索字符串 - 您需要单引号值 - 因此请在where子句中替换为'admin'
和'pass'
。