我通过以下代码不断获得功能不受支持的异常:
public Collection<Product> getProds(String col, String prod) {
LinkedList<Product> list = new LinkedList<>();
try {
String query = "SELECT * FROM Product where ? LIKE ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, col);
ps.setString(1, prod);
ResultSet rs = ps.executeQuery(query);
while ( rs.next() ) {
Product product = new Product(
rs.getString("PID"),
rs.getString("Artist"),
rs.getString("Title"),
rs.getString("Description"),
rs.getInt("price"),
rs.getString("thumbnail"),
rs.getString("fullimage")
);
list.add( product );
}
return list;
}
catch(Exception e) {
System.out.println( "Exception in getProducts(): " + e );
return null;
}
}
我想这与executeQuery有关。任何帮助深表感谢
最佳答案
您不能使用PreparedStatement的setString
方法设置列名,只能设置参数。
如果您不知道列名,则可以在创建PreparedStatement之前使用Java String的replace
方法指定列名,例如:
String query = "SELECT * FROM Product where <column> LIKE ?";
query = query.replaceAll("<column>", col);
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, prod);
ResultSet rs = ps.executeQuery();