简体   繁体   English

语法MySql插入错误

[英]Syntax MySql Insert Error

I got a Sql syntax error 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 我收到了一个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 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 ,(i take attribute by input),my table in mysql is,for example i post an insert: 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 . 插入其itshop supply ( id , idSupplier , dateTime , quantity , totalCost ) VALUES ('2', '4', '2009-1-4', '24', '245'); supplyididSupplierdateTimequantitytotalCost )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);
                    }

You need to add the apostrophes ' in the String which represents your INSERT statement. 您需要添加撇号'中,代表你的INSERT语句中的字符串。 Also, you need to add the right bracket ) . 此外,您需要添加右括号) You don't have them. 你没有它们。 Even better, use a PreparedStatement as this way of creating INSERT statements (by using direct concatenation of the values) is a bad practice. 更好的是,使用PreparedStatement作为这种创建INSERT语句的方式(通过使用值的直接连接)是一种不好的做法。

You need to keep the varchar types within '' like this 您需要将varchar类型保持在''这样的范围内

int rs2 = st2.executeUpdate("INSERT INTO supply VALUES ( '"
                                    + id + " ',' " + idSupplier + "' , '"
                                    + dateTime + " ', '" + idProduct + " ','"
                                    + Quantity + " ', '" + price + "'");

Use PreparedStatement instead of Statement and this removes the confusion of ' 使用PreparedStatement代替Statement,这消除了'混淆'

Simple example 简单的例子

PreparedStatement pt=connection.prepareStatement(insert into test values(?,?));
pt.setString(1,"hi");
pt.setInt(2,1);
pt.executeUpdate();

For your insert operation PreparedStatemet will be 对于您的插入操作,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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM