mysql> SELECT * FROM COFFEES WHERE COF_NAME IN ("Amaretto", "Espresso");
+----------+--------+-------+-------+-------+
| COF_NAME | SUP_ID | PRICE | SALES | TOTAL |
+----------+--------+-------+-------+-------+
| Amaretto | 49 | 9.99 | 0 | 0 |
| Espresso | 150 | 9.99 | 0 | 0 |
+----------+--------+-------+-------+-------+
2 rows in set (0.00 sec)
これらのPRICEをひとまとまりで更新してみます。
123456789101112131415161718192021222324
try{context=newInitialContext();DataSourcedataSource=(DataSource)context.lookup("java:comp/env/jdbc/continuousops");connection=dataSource.getConnection();connection.setAutoCommit(false);preparedStatement1=connection.prepareStatement("UPDATE COFFEES SET PRICE=? WHERE COF_NAME = 'Amaretto' LIMIT 1");preparedStatement2=connection.prepareStatement("UPDATE COFFEES SET PRICE=? WHERE COF_NAME = 'Espresso' LIMIT 1");preparedStatement1.setFloat(1,7.99f);preparedStatement1.executeUpdate();preparedStatement2.setFloat(1,6.99f);preparedStatement2.executeUpdate();connection.commit();}catch(NamingException|SQLExceptione){connection.rollback();e.printStackTrace();}finally{connection.setAutoCommit(true);}
Savepointsavepoint1=null;Savepointsavepoint2=null;try{context=newInitialContext();DataSourcedataSource=(DataSource)context.lookup("java:comp/env/jdbc/continuousops");connection=dataSource.getConnection();connection.setAutoCommit(false);savepoint1=connection.setSavepoint("point1");preparedStatement1=connection.prepareStatement("UPDATE COFFEES SET PRICE=? WHERE COF_NAME = 'Amaretto' LIMIT 1");preparedStatement2=connection.prepareStatement("UPDATE COFFEES SET PRICE=? WHERE COF_NAME = 'Espresso' LIMIT 1");preparedStatement1.setFloat(1,5.99f);preparedStatement1.executeUpdate();savepoint2=connection.setSavepoint("point2");preparedStatement2.setFloat(1,7.99f);preparedStatement2.executeUpdate();connection.rollback(savepoint2);connection.commit();}catch(NamingException|SQLExceptione){try{connection.rollback(savepoint1);