java - JDBC、MySQL -PreparedStatementexecuteUpdate 出现 DML 错误

标签 java mysql jdbc

我有两张 table 。我需要从第二个表中针对 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: Can not issue data manipulation statements with 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)

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

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

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

最佳答案

这里完全缺少您的备注专栏,

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

您的列备注此处未转义,

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

但是您应该使用bind parametersString StringEscapeUtils#escapeSql(String)因为您的代码容易受到 sql injection 的攻击.

关于java - JDBC、MySQL -PreparedStatementexecuteUpdate 出现 DML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22522964/

相关文章:

java - java 遍历二叉搜索树

mysql - Matlab 数据库工具箱 - 警告 : com. mysql.jdbc.Connection@6e544a45 不可序列化

java - JDBC 与 MySQL 的持久连接

java - Win Server Tomcat 8.0 上的 War 文件

java - 用于 Selenium 测试的 Spring Security @WithMockUser

java - 没有主体的接口(interface) stub 方法如何产生效果?

mysql - 在 MySQL 中将一对多关系重构为多对多 : How to formulate the query?

java - 表观死锁 c3p0 0.9.5.1 spring

Mysql join 与 left join 和 group by

PostgreSQL 错误代码未以 int 形式返回?