Java – specify the variable name in the query where clause in JDBC

Someone can give me a link about how to create a query in JDBC. The query obtains the variable name in the where statement, or write an example. More specifically, my code looks 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);
}

Now I need a syntax that allows me to return a resultset object where the URL in the CacheTable is equal to the URL of the method parameter

thank you

Solution

The easiest way is

String query = "select LastModified from CacheTable where url = '" + url +"'";

You should use bound variables:

String query = "select LastModified from CacheTable where url = ?";
prepStmt = conn.prepareStatement(query);
prepStmt.setString(1,url);
rs = prepStmt.executeQuery();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>