Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么JDBCMySQL会在您的SQL语法中抛出错误,尽管我生成的查询工作正常?_Java_Mysql_Sql_Database_Jdbc - Fatal编程技术网

Java 为什么JDBCMySQL会在您的SQL语法中抛出错误,尽管我生成的查询工作正常?

Java 为什么JDBCMySQL会在您的SQL语法中抛出错误,尽管我生成的查询工作正常?,java,mysql,sql,database,jdbc,Java,Mysql,Sql,Database,Jdbc,在我一直从事的项目中,有很多类似的代码块 DBConnector db = new DBConnector(); Connection conn = db.connect(); String insertQuery = "INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) " + "VALUES

在我一直从事的项目中,有很多类似的代码块

    DBConnector db = new DBConnector();
    Connection conn = db.connect();
    String insertQuery = "INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) " + "VALUES (?,?,?,?,?,?)";
    PreparedStatement pstmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
    pstmt.setInt(1, this.trackItem.getId());
    pstmt.setInt(2, this.node.getId());
    pstmt.setInt(3, this.tirDetailId);
    pstmt.setString(4, this.tirResultString);
    pstmt.setString(5, this.tirResultValue);
    pstmt.setString(6, this.tirStatus);
    System.out.println(pstmt.toString());
    pstmt.executeUpdate(insertQuery);

    ResultSet generatedKeys = pstmt.getGeneratedKeys();
我在上面代码的最后三行打印了查询,这是一个格式良好的SQL语句

com.mysql.cj.jdbc.ClientPreparedStatement: INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) VALUES (3,15,0,'<EOL><EOL><EOL><EOL>','-1','F')
这里的TrackItemResult.insertInDb(TrackItemResult.java:98)是pstmt.executeUpdate()行


我无法理解这种行为。这个查询看起来很正常,实际上是有效的。这些值根据架构正确格式化。还有什么可能导致这种情况?

请尝试
pstmt.executeUpdate()而不是
pstmt.executeUpdate(insertQuery)

尝试
pstmt.executeUpdate()而不是
pstmt.executeUpdate(insertQuery)

pstmt.executeUpdate();不需要参数

或者您应该检查sql的参数类型

pstmt.executeUpdate();不需要参数


或者您应该检查sql的参数类型

try
pstmt.executeUpdate()而不是
pstmt.executeUpdate(insertQuery)pstmt.executeUpdate()而不是
pstmt.executeUpdate(insertQuery)java.sql.SQLSyntaxErrorException: 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 '?,?,?,?,?,?)' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393)
    at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353)
    at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303)
    at com.example.myproject.models.TrackItemResult.insertInDb(TrackItemResult.java:98)
    at com.example.myproject.healthcheck.TrackItemRunnable.run(TrackItemRunnable.java:86)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)