我遇到了异常
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?http://www.andhrabhoomi.net/' at line 1
我正在尝试检查我的数据库中是否存在某个字符串
我的代码:
sql = "SELECT url FROM data where url = ?"+ "string";
ResultSet rs = statement.executeQuery(sql);
System.out.println(rs);
最佳答案
由于您确实有 ?
参数标记,因此您似乎正在尝试使用 PreparedStatement
,正如您应该做的那样,所有这些都假设 "string"
是在 ?
所在位置使用的实际值的经过清理的示例。
这是 PreparedStatement
应该如何完成的:
String sql = "SELECT url FROM data where url = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "string");
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString("url"));
}
}
}
关于java - java jdbc中如何查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36146935/