【发布时间】: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控制台错误吗?