【问题标题】:mysql update error when I reuse pstmt.executeUpdate in eclipse jsp file当我在eclipse jsp文件中重用pstmt.executeUpdate时mysql更新错误
【发布时间】:2016-05-01 12:14:20
【问题描述】:

我在 eclipse jsp 文件中使用 mysql update 时遇到问题。 我只是在我的 jsp 文件中使用 pstmt.executeUpdate 。那是正确的。 但是当我重用 pstmt.executeUpdate 来更新另一个主机的数据库时,我无法更新 mysql 数据。

当我更新第二个 executeUpdate 而没有更新的“where”查询时,它工作正常。我可以更新mysql数据。

但唯一的问题是,当我用“where”更新 executeUpdate 时,我无法更新 mysql 数据。

case1(没问题)

使用preparedStatement1,用'where'更新查询-> OK

使用preparedStatement2,更新不带'where'的查询-> OK

案例2(问题)

使用preparedStatement1,用'where'更新查询-> OK

使用preparedStatement2,用'where'更新查询->更新失败

(并且我已经在 mysql 工作台中测试了我的代码,第一次更新和第二次更新没有问题,我在 Eclipse 中添加了“where”代码的更新。)

后面是我的代码。

        String DB_USER = (String)session.getAttribute("id");
        String DB_PASSWORD = (String)session.getAttribute("password"); 
        String DB_HOST = "jdbc:mysql://rds01.********.ap-northeast-2.rds.amazonaws.com:3306/inventory_" + DB_USER + "?useUnicode=true&characterEncoding=euckr";

        Connection con;
        PreparedStatement pstmt;

        String query = "UPDATE inventory_"+DB_USER+" SET "
            + "color1_size1 = ?, color1_size2 = ?, color1_size3 = ?, color1_size4 = ?, color1_size5 = ?, color1_size6 = ?, color1_size7 = ?, color1_size8 = ? "
                        + "WHERE NAME = ? AND YEAR = ?";

    try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(DB_HOST, DB_USER, DB_PASSWORD);
            pstmt = con.prepareStatement(query);

            pstmt.setInt(1, color1_size1);
            pstmt.setInt(2, color1_size2);
            pstmt.setInt(3, color1_size3);
            pstmt.setInt(4, color1_size4);
            pstmt.setInt(5, color1_size5);
            pstmt.setInt(6, color1_size6);
            pstmt.setInt(7, color1_size7);
            pstmt.setInt(8, color1_size8);
            pstmt.setString(9, name);
            pstmt.setString(10, year);
            pstmt.executeUpdate();
            pstmt.close();
            con.close();

            String query_all = "UPDATE inventory_all SET "
                    + "color1_size1 = ?, color1_size2 = ?, color1_size3 = ?, color1_size4 = ?, color1_size5 = ?, color1_size6 = ?, color1_size7 = ?, color1_size8 = ? "
                        + "WHERE NAME = ? AND YEAR = ?";

    //behind host is another host with upper host.
            DB_HOST = "jdbc:mysql://rds01.************.ap-northeast-2.rds.amazonaws.com:3306/inventory_all?useUnicode=true&characterEncoding=euckr";
            con = DriverManager.getConnection(DB_HOST, DB_USER, DB_PASSWORD);
            pstmt = con.prepareStatement(query_all);

            pstmt.setInt(1, color1_size1);
            pstmt.setInt(2, color1_size2);
            pstmt.setInt(3, color1_size3);
            pstmt.setInt(4, color1_size4);
            pstmt.setInt(5, color1_size5);
            pstmt.setInt(6, color1_size6);
            pstmt.setInt(7, color1_size7);
            pstmt.setInt(8, color1_size8);
            pstmt.setString(9, name);
            pstmt.setString(10, year);

//------------upper code is OK, behind code is a problem ----------------
            pstmt.executeUpdate();
            pstmt.close();
            con.close();

    } catch(Exception e) {e.printStackTrace();}

【问题讨论】:

  • 那么错误是什么?
  • 我无法使用第二个 pstmt.executeUpdate() 更新 mysql 数据;
  • 当我用 "where" 更新 executeUpdate 时,我无法更新数据。 可能是您的 where 条件失败。
  • 是的,[where] 条件有问题。但我认为这段代码没有问题,因为相同的上层代码可以正常工作。代码完全一样。 :(
  • 你能告诉我们java控制台错误吗?

标签: java mysql eclipse jsp


【解决方案1】:

对第二个查询使用不同的 PreparedStatement 对象。因为 一个

preparedstatement 请求 sql 并立即自行编译。

PreparedStatement pstmt1 = con.prepareStatement(sql1);
pstmt1.executeUpdate();
PreparedStatement pstmt2 = con.prepareStatement(sql2);
pstmt2.executeUpdate();

【讨论】:

  • 用一条语句可以运行多个查询。
  • 是的,单个Statement对象可以用来执行多个查询。
  • 那么,是否需要再创建一个对象@Deepika Rajani
  • Statement 和 PreparedStatement 是两个不同的接口。 PreparedStatement 预编译查询,而在 Statement 中,查询不是预编译的。因此,如果我们对多个查询使用相同的 PreparedStatement 对象,它指的是第一个查询(已编译),而 Statement 实例并非如此。因此,我们可以对多个查询使用相同的语句,但不能使用 PreparedStatement。
【解决方案2】:

哦,我知道问题所在。我在数据库 id 中添加了仅更新、删除、插入而不是选择的 mysql 权限。 当我将“选择”权限添加到我的 ID 时,我可以在我的数据库中进行更新。 谢谢大家的回答!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多