尝试将查询请求实施到程序中时遇到了此问题。如果不使用此方法连接数据库,则工作正常,但是会抛出一个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!");
}
}
我将查询带入方法以隔离它。
任何帮助,将不胜感激。
最佳答案
通常搜索字符串-您需要将值单引号-因此在where子句中用'admin'
和'pass'
替换。
关于java - MySQL查询错误SQLException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21654091/