【发布时间】:2014-03-06 10:04:32
【问题描述】:
我在尝试将查询请求实现到程序中时遇到了这个问题。不使用此方法连接到数据库可以正常工作,但是,这会引发 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!");
}
}
我将查询引入到隔离它的方法中。 任何帮助将不胜感激。
【问题讨论】:
-
研究使用准备好的语句:Link:它们几乎消除了处理引用字符串/varchar 字段的麻烦。在您的情况下,
admin和pass没有正确地用单引号括起来。
标签: java jquery mysql sqlexception