这是我的源代码。

    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和draw字段添加值。请帮帮我。
![在此处输入图像描述][2]

最佳答案

order是SQL中的一个保留字,因此如果要将它用作表名,则需要用前引号保护它:

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

关于java - 如何修复此MySQL语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26794073/

10-12 06:58