我得到了一个Sql语法错误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 '8 , 0' at line 1
,(我通过输入获取属性),我在mysql中的表是,例如我发布了一个插入:
INSERT INTO itshop
。supply
(id
,idSupplier
,dateTime
,quantity
,totalCost
)VALUES(' 2','4','2009-1-4','24','245');
int rs2 = st2.executeUpdate("INSERT INTO supply VALUES ( "
+ id + " , " + idSupplier + " , "
+ dateTime + " , " + idProduct + " ,"
+ Quantity + " , " + price + "");
if (rs2 == 1) {
JOptionPane.showMessageDialog(Sale,
"Product Ordered", "Supply",
JOptionPane.DEFAULT_OPTION);
}
答案 0 :(得分:1)
您需要在表示INSERT语句的String中添加撇号'
。此外,您需要添加右括号)
。你没有它们。更好的是,使用PreparedStatement
作为创建INSERT
语句的方式(通过使用值的直接连接)是一种不好的做法。
答案 1 :(得分:1)
您需要将varchar类型保留在''
之内
int rs2 = st2.executeUpdate("INSERT INTO supply VALUES ( '"
+ id + " ',' " + idSupplier + "' , '"
+ dateTime + " ', '" + idProduct + " ','"
+ Quantity + " ', '" + price + "'");
使用PreparedStatement代替Statement,这消除了'
简单示例
PreparedStatement pt=connection.prepareStatement(insert into test values(?,?));
pt.setString(1,"hi");
pt.setInt(2,1);
pt.executeUpdate();
对于您的插入操作,PreparedStatemet将是
PreparedStatement pt=connection.prepareStatement("INSERT INTO supply VALUES (?,?,?,?,?,?)")
pt.setString(1,id );
pt.setString(2,idSupplier );
pt.setString(3,dateTime );
pt.setString(4,idProduct );
pt.setString(5,Quantity );
pt.setString(6,price );
int rs2=pt.executeUpdate();