Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在eclipse jsp文件中重用pstmt.executeUpdate时出现mysql更新错误_Java_Mysql_Eclipse_Jsp - Fatal编程技术网

Java 在eclipse jsp文件中重用pstmt.executeUpdate时出现mysql更新错误

Java 在eclipse jsp文件中重用pstmt.executeUpdate时出现mysql更新错误,java,mysql,eclipse,jsp,Java,Mysql,Eclipse,Jsp,我在EclipseJSP文件中使用mysql更新时遇到问题。 我只是在jsp文件中使用pstmt.executeUpdate。这是正确的工作。 但当我重用pstmt.executeUpdate来更新另一台主机的数据库时,我无法更新mysql数据 当我在没有更新的“where”查询的情况下更新secondexecuteupdate时,它就正常工作了。我可以更新mysql数据 但唯一的问题是,当我用“where”更新executeUpdate时,我无法更新mysql数据 案例1(没问题) 使用pre

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

当我在没有更新的“where”查询的情况下更新secondexecuteupdate时,它就正常工作了。我可以更新mysql数据

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

案例1(没问题)

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

使用preparedStatement2,更新查询时不带“where”->OK

案例2(问题)

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

使用preparedStatement2,使用'where'->update fail更新查询

(我已经在mysql workbench中测试了我的代码,第一次更新和第二次更新“where”都没有问题。我只是在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();}

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

preparedstatement请求sql并立即编译自身


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

那么错误是什么呢?我无法使用第二个pstmt.executeUpdate()更新mysql数据;当我用“where”更新executeUpdate时,我无法更新数据。可能是您的
,其中
条件失败。是,[where]条件有问题。但是我认为这段代码没有问题,因为上面的代码是一样的。代码完全相同(您能告诉我们java控制台错误吗?使用单个语句可以运行多个查询。是的,单个语句对象可以用于执行多个查询。那么,是否需要再创建一个对象@Deepika RajaniStatement和PreparedStatement是两个不同的接口。PreparedStatement预编译查询,而在语句中ery不是预编译的。因此,如果我们对多个查询使用相同的PreparedStatement对象,它指的是第一个查询(已编译),而语句实例则不是。因此,我们可以对多个查询使用相同的语句,但不能使用PreparedStatement。@Deepika Rajani
PreparedStatement pstmt1 = con.prepareStatement(sql1);
pstmt1.executeUpdate();
PreparedStatement pstmt2 = con.prepareStatement(sql2);
pstmt2.executeUpdate();