gpt4 book ai didi

java - 如何修复此 MySQL 语法错误

转载 作者:行者123 更新时间:2023-11-29 01:56:44 25 4
gpt4 key购买 nike

这是我的源代码。

    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

这是我的订单表。

Order table


在此移动中,我没有向 lot_id 和绘制字段添加值。请帮我。
![在此处输入图片描述][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/

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