[英]MySQL Query Error SQLException
I encountered this issue when trying to implement query requests into a program. 尝试将查询请求实现到程序中时遇到此问题。 Connecting to the database without this method works fine, however, this throws a SQLException
. 没有这种方法连接到数据库工作正常,但是,这会引发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!");
}
}
I brought the query into the method to isolate it. 我将查询带入了隔离它的方法。 Any help would be appreciated. 任何帮助,将不胜感激。
通常搜索字符串 - 您需要单引号值 - 因此在where子句中用'admin'
和'pass'
替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.