【问题标题】:JDBC, MySQL - DML Error with PreparedStatement executeUpdateJDBC、MySQL - PreparedStatement 执行更新的 DML 错误
【发布时间】:2014-04-26 16:58:44
【问题描述】:

我有两张桌子。我需要从第二个表中针对 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) 在 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);

错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '' 附近使用正确的语法 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 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.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)

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

【问题讨论】:

  • 发布您的整个例外情况。
  • 这是您正在运行的实际代码吗? while(it.hasNext){ 在我看来很奇怪。
  • 张贴您的实际代码。 String feature = (String) featuresIT.next(); // <-- Not it?+ colVal //<-- Not feature? 为什么不使用绑定参数?
  • @DavidWallace 我正在转换为字符串。我只是在示例代码中引用它。我将发布原始代码。
  • @DavidWallace 使用 PreparedStatement 但将常量推入查询中(这将使语句缓存无用),甚至更糟糕的是sql injection 漏洞。

标签: java mysql jdbc


【解决方案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的攻击。

【讨论】:

  • 我刚刚注意到通过打印查询语句。感谢您指出了这一点。我会听从你对编码标准的建议。
【解决方案2】:

关于第二个问题 - 您将四个值插入到五列中。你忘了 “备注”。

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

【讨论】:

  • 感谢您的建议。我包括了整个代码。能否请您刷新并检查。我相信在解决我的第一个问题后它会正确执行。我忽略了错误消息。
  • 是的,我在发布此答案之前阅读了您的代码。谢谢。您可以判断,因为我的回答是指您的最新编辑,而不是您发布的原始代码。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多