cachedRowSet=newCachedRowSetImpl();cachedRowSet.setDataSouceName("java:comp/env/jdbc/continuousops");cachedRowSet.setCommand("SELECT * FROM COFFEES");cachedRowSet.execute();
//getting first cachedRowSetcachedRowSet=newCachedRowSetImpl();cachedRowSet.setCommand("SELECT * FROM COFFEES");cachedRowSet.execute(connection);//getting second cachedRowSetcachedRowSet2=newCachedRowSetImpl();cachedRowSet2.setCommand("SELECT * FROM COFFEES");cachedRowSet2.execute(connection);cachedRowSet.next();cachedRowSet.updateInt("SUP_ID",101);cachedRowSet.updateRow();cachedRowSet.acceptChanges();cachedRowSet2.next();cachedRowSet2.updateInt("SUP_ID",150);cachedRowSet2.updateRow();cachedRowSet2.acceptChanges();
coffees=newCachedRowSetImpl();coffees.setCommand("SELECT * FROM COFFEES");coffees.execute(connection);suppliers=newCachedRowSetImpl();suppliers.setCommand("SELECT * FROM SUPPLIERS");suppliers.execute(connection);jrs=newJoinRowSetImpl();jrs.addRowSet(coffees,"SUP_ID");jrs.addRowSet(suppliers,"SUP_ID");System.out.println("Coffees bought from "+supplierName+": ");while(jrs.next()){if(jrs.getString("SUP_NAME").equals(supplierName)){StringcoffeeName=jrs.getString(1);System.out.println(" "+coffeeName);}}
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);
privatevoidupdateTable(Connectionconnection,StringdbName)throwsSQLException{floatpercentage=0.3f;Stringquery="SELECT cof_name, sup_id, price, sales, total FROM "+dbName+".coffees";preparedStatement=connection.prepareStatement(query);resultSet=preparedStatement.executeQuery();resultSet.next();System.out.println("|"+resultSet.getString("COF_NAME")+"|"+resultSet.getInt("SUP_ID")+"|"+resultSet.getBigDecimal("PRICE")+"|");floatf=resultSet.getFloat("PRICE");resultSet.updateFloat("PRICE",f*percentage);resultSet.updateRow();System.out.println("|"+resultSet.getString("COF_NAME")+"|"+resultSet.getInt("SUP_ID")+"|"+resultSet.getBigDecimal("PRICE")+"|");}
privatevoidupdateTableRevised(Connectionconnection,StringdbName)throwsSQLException{floatpercentage=0.3f;Stringquery="SELECT cof_name, sup_id, price, sales, total FROM "+dbName+".coffees";Statementstatement=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);resultSet=statement.executeQuery(query);resultSet.next();System.out.println("|"+resultSet.getString("COF_NAME")+"|"+resultSet.getInt("SUP_ID")+"|"+resultSet.getBigDecimal("PRICE")+"|");floatf=resultSet.getFloat("PRICE");resultSet.updateFloat("PRICE",f*percentage);resultSet.updateRow();System.out.println("|"+resultSet.getString("COF_NAME")+"|"+resultSet.getInt("SUP_ID")+"|"+resultSet.getBigDecimal("PRICE")+"|");}
packagecom.kirishikistudios.continuousops.digintojdbc;importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.naming.NamingException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.sql.DataSource;importjava.io.IOException;importjava.sql.Connection;importjava.sql.SQLException;publicclassChapter2extendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setAttribute("subject","JDBC");Contextcontext;Connectionconnection=null;try{context=newInitialContext();DataSourcedataSource=(DataSource)context.lookup("java:comp/env/jdbc/continuousops");connection=dataSource.getConnection();request.setAttribute("message1","Successfully connected to database.");connection.close();}catch(NamingExceptione){e.printStackTrace();}catch(SQLExceptione){e.printStackTrace();}finally{if(connection!=null){try{connection.close();request.setAttribute("message2","Successfully disconnected from database.");}catch(SQLExceptione){e.printStackTrace();}}}this.getServletContext().getRequestDispatcher("/chapter2/index.jsp").forward(request,response);}}