【问题标题】:I want to search for a product by its name and display the name and selling price in a JTextField in java [closed]我想按名称搜索产品并在java中的JTextField中显示名称和售价[关闭]
【发布时间】:2018-07-01 12:18:51
【问题描述】:
"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

【问题讨论】:

  • 请将您的错误消息和代码以代码格式的文本形式发布。否则我们很难复制、粘贴和尝试运行它。此外,由于防火墙限制,这里有些看不到图像。还请在您的问题中提供更多详细信息,包括解释性文本和背景信息——任何有助于我们更好地理解您的代码、问题和问题的内容。请通过How to Ask
  • 这是错误消息“您的 sql 语句有错误,请查看与您的 MariaDB 服务器版本相对应的手册,以了解在 '?' 附近使用的正确语法。在第 1 行

标签: java sql database


【解决方案1】:

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

所以代码的基础是:

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

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

ResultSet rs = stmt.executeQuery();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-16
    • 2014-06-22
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多