在JDBC中的QUERY WHERE子句中指定变量名称 [英] Specifying a variable name in QUERY WHERE clause in JDBC
本文介绍了在JDBC中的QUERY WHERE子句中指定变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以给我一个关于如何在JDBC中创建查询的链接,该查询在WHERE语句中获取变量名,或者编写一个示例,更具体一点,我的代码看起来像这样:
Could someone please give me a link on how to create a query in JDBC that gets a variable name in the WHERE statement, or write an example, to be more specific, my code looks something like this:
private String getLastModified(String url) {
String lastModified = null;
ResultSet resultSet;
String query = "select LastModified from CacheTable where " +
" URL.equals(url)";
try {
resultSet = sqlStatement.executeQuery(query);
}
现在我需要的语法能让我返回一个ResultSet对象,其中URL在cacheTable等于方法参数的url。
Now I need the syntax that enables me to return a ResultSet object where URL in the cacheTable equals url from the method's argument.
谢谢
推荐答案
最简单的方法是
String query = "select LastModified from CacheTable where url = '" + url +"'";
你应该使用绑定变量:
String query = "select LastModified from CacheTable where url = ?";
prepStmt = conn.prepareStatement(query);
prepStmt.setString(1, url);
rs = prepStmt.executeQuery();
这篇关于在JDBC中的QUERY WHERE子句中指定变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文