Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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 JDBC,MySQL-PreparedStatement executeUpdate出现DML错误_Java_Mysql_Jdbc - Fatal编程技术网

Java JDBC,MySQL-PreparedStatement executeUpdate出现DML错误

Java JDBC,MySQL-PreparedStatement executeUpdate出现DML错误,java,mysql,jdbc,Java,Mysql,Jdbc,我有两张桌子。我需要根据col2从第二个表中获取列表中的一些列值,并在循环中动态更新第一个表中的列值 代码: int ownerUpdateCount = 0; PreparedStatement statement = null; Connection connection = null; try{ while(featuresIT.hasNext()){ String feature = (String) featuresIT.

我有两张桌子。我需要根据col2从第二个表中获取列表中的一些列值,并在循环中动态更新第一个表中的列值

代码:

int ownerUpdateCount = 0;
    PreparedStatement statement = null;
    Connection connection = null;
    try{
        while(featuresIT.hasNext()){
            String feature = (String) featuresIT.next();

            String updateOwnerQuery = "UPDATE `regression_reports` "
                    + "JOIN `feature_testbed_owner_mapping_628` ON (`regression_reports`.Feature='"
                    + feature
                    + "' AND `regression_reports`.Feature = `feature_testbed_owner_mapping_628`.feature_as_on_webpage) "
                    + "SET `regression_reports`.Owner = `feature_testbed_owner_mapping_628`.owner";
            //logger.debug("\n updateOwnerQuery : " + updateOwnerQuery);
            connection = dataSource.getConnection();
            connection.setAutoCommit(true);
            statement = connection.prepareStatement(updateOwnerQuery);
            //ownerUpdateCount += 
            ownerUpdateCount = statement.executeUpdate();
            System.out.println(feature + " ownerUpdateCount : " + ownerUpdateCount);
        }
        logger.debug("\n ownerUpdateCount : " + ownerUpdateCount);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
           try { if(null!=statement)statement.close();} catch (SQLException e) 
           {e.printStackTrace();}
           try { if(null!=connection)connection.close();} catch (SQLException e) 
           {e.printStackTrace();}
    }
错误:

int ownerUpdateCount = 0;
    PreparedStatement statement = null;
    Connection connection = null;
    try{
        while(featuresIT.hasNext()){
            String feature = (String) featuresIT.next();

            String updateOwnerQuery = "UPDATE `regression_reports` "
                    + "JOIN `feature_testbed_owner_mapping_628` ON (`regression_reports`.Feature='"
                    + feature
                    + "' AND `regression_reports`.Feature = `feature_testbed_owner_mapping_628`.feature_as_on_webpage) "
                    + "SET `regression_reports`.Owner = `feature_testbed_owner_mapping_628`.owner";
            //logger.debug("\n updateOwnerQuery : " + updateOwnerQuery);
            connection = dataSource.getConnection();
            connection.setAutoCommit(true);
            statement = connection.prepareStatement(updateOwnerQuery);
            //ownerUpdateCount += 
            ownerUpdateCount = statement.executeUpdate();
            System.out.println(feature + " ownerUpdateCount : " + ownerUpdateCount);
        }
        logger.debug("\n ownerUpdateCount : " + ownerUpdateCount);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
           try { if(null!=statement)statement.close();} catch (SQLException e) 
           {e.printStackTrace();}
           try { if(null!=connection)connection.close();} catch (SQLException e) 
           {e.printStackTrace();}
    }

java.sql.SQLException:无法使用executeQuery()发出数据操作语句。 位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) 位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) 位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) 位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) 位于com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:499) 在com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1518) 位于org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208) 位于org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208) 位于com.n7k.regression.RegressionDAO.updateRegReportsDB(RegressionDAO.java:71) 位于com.n7k.regression.RegressionServlet.doPost(RegressionServlet.java:63)

此错误仅在循环的第一次迭代中发生。任何关于调试其发生原因的建议

String updateFeaturesQuery = "INSERT INTO `regression_reports` "
                    + "(Feature, `Report`, `P`, `F`, `Remarks`) VALUES ('"
                    + feature + "','" + report + "'," + pass + "," + fail
                    + ") " + "ON DUPLICATE KEY UPDATE `Report` = '"
                    + report + "', `P` = " + pass + ", `F`= " + fail
                    + ", `Remarks` = " + remarks + ";";

       connection = dataSource.getConnection();
       statement = connection.prepareStatement(updateFeaturesQuery);

       Line#73 
       featureUpdateCount = statement.executeUpdate(updateFeaturesQuery);
错误:

int ownerUpdateCount = 0;
    PreparedStatement statement = null;
    Connection connection = null;
    try{
        while(featuresIT.hasNext()){
            String feature = (String) featuresIT.next();

            String updateOwnerQuery = "UPDATE `regression_reports` "
                    + "JOIN `feature_testbed_owner_mapping_628` ON (`regression_reports`.Feature='"
                    + feature
                    + "' AND `regression_reports`.Feature = `feature_testbed_owner_mapping_628`.feature_as_on_webpage) "
                    + "SET `regression_reports`.Owner = `feature_testbed_owner_mapping_628`.owner";
            //logger.debug("\n updateOwnerQuery : " + updateOwnerQuery);
            connection = dataSource.getConnection();
            connection.setAutoCommit(true);
            statement = connection.prepareStatement(updateOwnerQuery);
            //ownerUpdateCount += 
            ownerUpdateCount = statement.executeUpdate();
            System.out.println(feature + " ownerUpdateCount : " + ownerUpdateCount);
        }
        logger.debug("\n ownerUpdateCount : " + ownerUpdateCount);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
           try { if(null!=statement)statement.close();} catch (SQLException e) 
           {e.printStackTrace();}
           try { if(null!=connection)connection.close();} catch (SQLException e) 
           {e.printStackTrace();}
    }
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以获取第1行“”附近要使用的正确语法 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:513) 位于com.mysql.jdbc.Util.HandleneInstance(Util.java:411) 位于com.mysql.jdbc.Util.getInstance(Util.java:386) 位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)上 在com.mysql.jdbc.MysqlIO.checkErrorPacket上(MysqlIO.java:4122) 位于com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) 位于com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) 位于com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812) 位于com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) 位于com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) 位于org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228) 位于org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228) 位于com.n7k.regression.RegressionDAO.updateRegReportsDB(RegressionDAO.java:73)


上面的执行给出了这个错误。手动执行相同的查询不会给我带来任何错误。

您的专栏
备注
在此完全缺失

// No `Remarks` even though you included it in the list. `F`, `Remarks`)
 "," + fail /* + Remarks! */ + ") " + "ON DUPLICATE KEY UPDATE `Report` = '"

您的列
备注
不会在此处转义

+ ", `Remarks` = '" + remarks + "';";

但是您应该使用or,因为您的代码易受攻击。

是第二个问题-您在五列中插入了四个值。你忘了 “备注”


您是第一个问题-您没有向我们展示正确的代码。

您在sql查询中犯了一些错误,比如
并且您缺少像这样获取的值
备注

  STRING updateFeaturesQuery = 
                 "INSERT INTO `regression_reports` "

                + "(Feature,      `Report`,        `P`,         `F`, `Remarks`) 
                VALUES 
                ('"
                + feature + "','"+report +"',"+ pass +","+ fail+",/*"+ Remarks+"*/ 
                + ") " + 

                "ON DUPLICATE KEY UPDATE 
                `Report` = '"+ report + "',
                 `P` = " + pass + ", 
                `F`= " + fail+ ", 
                `Remarks` = " + remarks + "/*;*/";

请发布您的整个异常。这是您正在运行的实际代码吗
while(it.hasNext){
在我看来很奇怪。请发布您的实际代码。
String feature=(String)featuresIT.next();/@DavidWallace我正在转换为String。我只是引用它作为示例代码。我将发布原始代码。@DavidWallace使用PreparedStatement,但将常量推入查询中(这将使语句缓存无效)还有更糟糕的漏洞。我刚刚通过打印查询语句注意到了。感谢您指出这一点。我将按照您的建议制定编码标准。感谢您的建议。我包含了整个代码。请您刷新并检查。我相信在解决第一个问题后它将正确执行。我忽略了错误是的,我在发布此答案之前阅读了您的代码。谢谢。您可以看出,因为我的答案是指您最近的编辑,而不是您发布的原始代码。