简体   繁体   English

JDBC,MySQL-带PreparedStatement executeUpdate的DML错误

[英]JDBC, MySQL - DML Error with PreparedStatement executeUpdate

I have two tables. 我有两张桌子。 I need to get some column values in a List from second table against col2 and update in the first table on the fly in a loop. 我需要针对col2从第二个表中获取列表中的某些列值,并在循环中动态地对第一个表进行更新。

Code : 代码:

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();}
    }

Error : 错误:


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

This error occurs only in the first iteration of the loop. 该错误仅在循环的第一次迭代中发生。 Any suggestion to debug why its happening. 任何有关调试其原因的建议。

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);

Error : 错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)的第1行附近使用com上的java.lang.reflect.Constructor.newInstance(Constructor.java:513)上的sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)com上的com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 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.sendCommand(MysqlIO.java:2570)上的com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812)在com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228) at com.n7k.regression.RegressionDAO.updateRegReportsDB(RegressionDAO.java:73) 在com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)在org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)在org.apache.tomcat.dbcp.dbcp.DelegatingStatement .comuteUpdate(DelegatingStatement.java:228)位于com.n7k.regression.RegressionDAO.updateRegReportsDB(RegressionDAO.java:73)

The above execution gives this error. 上面的执行给出了这个错误。 Executing the same query manually doesn't throw me any error. 手动执行相同的查询不会引发任何错误。

Your column Remarks is missing entirely here, 您的专栏“ Remarks在这里完全丢失,

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

Your column Remarks is not escaped here, 您的专栏“ Remarks不在此处转义,

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

But you should use bind parameters or String StringEscapeUtils#escapeSql(String) because your code is vulnerable to sql injection . 但是您应该使用绑定参数String StringEscapeUtils#escapeSql(String),因为您的代码容易受到sql注入的攻击。

Re the second problem - you are inserting four values into five columns. 再说第二个问题-您将四个值插入五列。 You forgot "Remarks". 您忘记了“备注”。

Re the first problem - you haven't shown us the right piece of code. 关于第一个问题-您没有给我们展示正确的代码。

you doing some mistake you sql query, like ; 您在执行sql查询时出错,例如; and you are missing value to get as like Remarks . 并且您缺少获得像Remarks一样的价值。

  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 + "/*;*/";

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

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