Java 获得;jdbc.SQLServerException:靠近';的语法不正确'&引用;执行PreparedStatement时出错

Java 获得;jdbc.SQLServerException:靠近';的语法不正确'&引用;执行PreparedStatement时出错,java,sql,sql-server,jdbc,Java,Sql,Sql Server,Jdbc,我编写了一些java代码,在用户按下按钮时将数据插入SQL Server 2012的数据库。运行代码时,出现以下错误: com.microsoft.sqlserver.jdbc.SQLServerException:靠近“,”的语法不正确 它说,sqlstatement.executeUpdate()行导致错误。我知道这条线不是问题。问题是我的sql查询,但我无法找到我的查询是如何错误的。你能帮帮我吗 这是密码 count++; for(int count = 0; count < tab

我编写了一些java代码,在用户按下按钮时将数据插入SQL Server 2012的数据库。运行代码时,出现以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException:靠近“,”的语法不正确

它说,
sqlstatement.executeUpdate()行导致错误。我知道这条线不是问题。问题是我的sql查询,但我无法找到我的查询是如何错误的。你能帮帮我吗

这是密码

count++;
for(int count = 0; count < table_1.getRowCount(); count++){
    try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          Connection dbconbt8 = DriverManager.getConnection("" +"jdbc:sqlserver://localhost;databaseName=Store;user=sa;password=XXXXXX");
          String sqlQ = "INSERT INTO [dbo].[Transaction]([TransactionID],[ProductID]"+
           ",[TotalPrice]) VALUES ("+count+"','"+table_1.getValueAt(count, 0).toString()+"','"+sumprice+ "') ";
           PreparedStatement sqlstatement = dbconbt8.prepareStatement(sqlQ);
           sqlstatement.executeUpdate();
                       sqlstatement.close();
                       dbconbt8.close();
            } catch (SQLException e1) {

                          e1.printStackTrace();
                      } catch (ClassNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                  }
count++;
对于(int count=0;count
值(
)之后缺少一个引号-这应该可以解决问题:

String sqlQ = "INSERT INTO [dbo].[Transaction]([TransactionID],[ProductID]"+
    ",[TotalPrice]) VALUES ('"+count+"','"+table_1.getValueAt(count, 0).toString()+"','"+sumprice+ "') ";
--                          ^
--                        Here
但是,这是一个错误的解决方案:您应该使用参数重写查询,以便引用数据的问题变得完全无关:

String sqlQ = "INSERT INTO [dbo].[Transaction]([TransactionID],[ProductID],[TotalPrice]) VALUES (?,?,?) ";
PreparedStatement sqlstatement = dbconbt8.prepareStatement(sqlQ);
sqlstatement.setInt(1, count);
sqlstatement.setString(2, table_1.getValueAt(count, 0).toString());
sqlstatement.setInt(3, sumprice);
sqlstatement.executeUpdate();

还要去更改您的“sa”密码!您……似乎真的不明白PreparedStatement的意义,所以它(几乎)是有效的t-SQL?@ElliottFrisch除了容易受到SQL注入攻击之外,是的。t-SQL使用方括号作为名称的“引号”。