【问题标题】:How to fix this MySQL syntax error如何修复这个 MySQL 语法错误
【发布时间】:2023-04-02 06:12:01
【问题描述】:

这是我的源代码。

    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {     
    public void saveOrder(JTable jTable2, JDateChooser jd) {
    if (jTable2.getRowCount() > 0) {

        for (int i = 0; i < jTable2.getRowCount(); i++) {
            int lot_id = Integer.parseInt(jTable2.getValueAt(i, 0).toString());
            String lot_date = jTable2.getValueAt(i, 1).toString();
            String lot_type = jTable2.getValueAt(i, 2).toString();
            int draw = Integer.parseInt(jTable2.getValueAt(i, 4).toString());
            int f_order = Integer.parseInt(jTable2.getValueAt(i, 5).toString());
            int qty = Integer.parseInt(jTable2.getValueAt(i, 6).toString());
            double cost = Double.parseDouble(jTable2.getValueAt(i, 7).toString());
            double profit = Double.parseDouble(jTable2.getValueAt(i, 8).toString());


                    try {
                    boolean b = JDBC.putData("insert into order(lot_date, inst_id, qty, total, ptotal) values('"+lot_date+"', '"+lot_id+"', '"+qty+"', '"+cost+"', '"+profit+"' )");

                    if (b) {
                        JOptionPane.showMessageDialog(null, "invoice saved one");

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


    } else {
        JOptionPane.showMessageDialog(null, "Enter Item to Invoice", "Error",                   JOptionPane.ERROR_MESSAGE);
    }
}} 



这是我的 JDBC 类。

package Modle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


  public class JDBC {
  static Connection con;
  static boolean b;
  public static void setCon() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/lottery", "root", "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public static Connection getCon() throws Exception{
    if (con==null) {
        setCon();
    } 
        return con;
  }

public static boolean putData(String sql){
    try {
        PreparedStatement state = getCon().prepareStatement(sql);
        state.executeUpdate();
        b=true;
    } catch (Exception e) {
        e.printStackTrace();
    b=false;
    }
    return b;
   }

}

当我按下此按钮时,出现语法错误异常。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order(lot_date, inst_id, qty, total, ptotal) values('2014-11-14', '4', '5', '100' at line 1

这是我的订单表。


在这个动作中,我没有向 lot_id 和绘制字段添加值。请帮我。
![在此处输入图片描述][2]

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    order是SQL中的保留字,所以如果你想用它作为表名你需要用正向引号保护它:

    boolean b = JDBC.putData("insert into `order` (lot_date, inst_id, qty, total, ptotal) values ('"+lot_date+"', '"+lot_id+"', '"+qty+"', '"+cost+"', '"+profit+"' )");
    

    【讨论】:

      【解决方案2】:

      Mureinik's answer is correct.

      这与错误无关,但是当您已经在使用 PreparedStatement 时,请使用占位符 (?) 来设置值。 这可以防止SQL Injection 攻击。

      更新

      Why do I use PreparedStatement?

      原因很简单:

      • 在内联绑定值时,您可以忽略源自错误字符串连接的语法错误。
      • 在内联绑定值时,您可以从错误的字符串连接中忽略 SQL 注入漏洞。
      • 内联更“复杂”的数据类型(例如 TIMESTAMP、二进制数据等)时,您可以避免边缘情况。
      • 您可以将打开的 PreparedStatements 保持一段时间,用新的绑定值重新使用它们,而不是立即关闭它们(例如,在 Postgres 中很有用)。
      • 您可以在更复杂的数据库中使用自适应游标共享 (Oracle-speak)。这有助于防止对每组新的绑定值进行硬解析 SQL 语句。

      另见

      【讨论】:

      • 我认为在每个插入代码中添加准备好的语句会减慢系统速度。不是吗?感谢你的信息。没人告诉我。
      猜你喜欢
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2017-08-09
      • 2022-12-17
      相关资源
      最近更新 更多