我想按产品名称搜索产品,并在Java的JTextField中显示名称和售价

时间:2018-07-01 12:18:51

标签: java sql database

"SELECT * FROM products WHERE product_name = ?";

This is the error message i am getting

This is my code

String sql = "SELECT * FROM posdb.products WHERE products.product_name = ?";
PreparedStatement ps = myCon.CreateConnection().prepareStatement(sql);

这是我收到的错误消息:

You have an error in your sql statement, check the manual that 
  corresponds to your MariaDB server version for the right 
  syntax to use near '?' at line 1

1 个答案:

答案 0 :(得分:0)

使用PreparedStatement时,您需要替换每个“?”在执行实际查询之前输入有效值。

所以代码的基础是:

String sql = "Select * from SomeTable where SomeColumn = ?";

PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, someColumnVariable);

ResultSet rs = stmt.executeQuery();