JDBC,MySQL - 与PreparedStatement executeUpdate的DML错误

时间:2014-03-20 03:36:09

标签: java mysql jdbc

我有两张桌子。我需要从第二个表中的列表中获取一些列值,而不是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();}
    }

错误:


  

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)       at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)       at 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);

错误:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第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)       在com.mysql.jdbc.Util.handleNewInstance(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)

上述执行会出现此错误。手动执行相同的查询不会给我带来任何错误。

3 个答案:

答案 0 :(得分:1)

您的专栏Remarks完全遗失在这里,

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

您的专栏Remarks未在此处进行转义,

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

使用bind parametersString StringEscapeUtils#escapeSql(String),因为您的代码容易受sql injection攻击。

答案 1 :(得分:0)

重新解决第二个问题 - 您要将四个值插入五列。你忘了 "备注"

重新解决第一个问题 - 您还没有向我们展示正确的代码。

答案 2 :(得分:0)

你在sql查询中犯了一些错误,比如;,你错过了得到的价值 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 + "/*;*/";