gpt4 book ai didi

java - SQL 功能不支持异常

转载 作者:行者123 更新时间:2023-11-29 19:13:48 24 4
gpt4 key购买 nike

我不断收到功能不支持的异常,代码如下:

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();

关于java - SQL 功能不支持异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com