A2: | Add this bit of code after the preceding in ProcessTransaction.jsp: //Now to demonstrate the use of batch updates: conn.setAutoCommit(false); Statement batchUpdateStmt = conn.createStatement(); int batchTransId = intTransactionID + 1; batchUpdateStmt.addBatch("Insert into transaction_details (transaction_id , customer_name , total_amount , creditcard_no , card_type)values("+batchTransId+",'Joe Smith',100, '1111222233334444','Visa')"); batchTransId++; batchUpdateStmt.addBatch("Insert into transaction_details (transaction_id , customer_name , total_amount , creditcard_no , card_type)values("+batchTransId+",'Alan Smith',200, '2222333344441111','MasterCard')"); batchTransId++; batchUpdateStmt.addBatch("Insert into transaction_details (transaction_id , customer_name , total_amount , creditcard_no , card_type)values("+batchTransId+",'Bijoy Smith',300, '3333444411112222','Amex')"); batchTransId++; batchUpdateStmt.addBatch("Insert into transaction_details (transaction_id , customer_name , total_amount , creditcard_no , card_type)values("+batchTransId+",'Poonam Smith',400, '4444111122223333','Discover')"); batchTransId++; batchUpdateStmt.addBatch("Insert into transaction_details (transaction_id , customer_name , total_amount , creditcard_no , card_type)values("+batchTransId+",'Dillon Smith',500, '2222111133334444','Visa')"); batchTransId++; batchUpdateStmt.addBatch("Insert into transaction_details (transaction_id , customer_name , total_amount , creditcard_no , card_type)values("+batchTransId+",'Nancy Smith',600, '3333111122224444','MasterCard')"); int[] updateCount = batchUpdateStmt.executeBatch(); conn.commit(); //Using prepared statement for batch updates: conn.setAutoCommit(false); PreparedStatement prepBatchStmt = conn.prepareStatement("Insert into transaction_details (transaction_id, customer_name, total_amount , creditcard_no , card_type)values(?,?,?,?,?)"); prepBatchStmt.setInt(1,batchTransId++); prepBatchStmt.setString(2,"Ronald Smith"); prepBatchStmt.setDouble(3,230); prepBatchStmt.setString(4,"1112122233334444"); prepBatchStmt.setString(5,"Visa"); prepBatchStmt.addBatch(); prepBatchStmt.setInt(1,batchTransId++); prepBatchStmt.setString(2,"Zinta Smith"); prepBatchStmt.setDouble(3,120); prepBatchStmt.setString(4,"1111222323334444"); prepBatchStmt.setString(5,"MasterCard"); prepBatchStmt.addBatch(); prepBatchStmt.setInt(1,batchTransId++); prepBatchStmt.setString(2,"Minal Smith"); prepBatchStmt.setDouble(3,50); prepBatchStmt.setString(4,"1111222233343444"); prepBatchStmt.setString(5,"Discover"); prepBatchStmt.addBatch(); prepBatchStmt.setInt(1,batchTransId++); prepBatchStmt.setString(2,"Ronald Smith"); prepBatchStmt.setDouble(3,30); prepBatchStmt.setString(4,"1112122244443333"); prepBatchStmt.setString(5,"Amex"); prepBatchStmt.addBatch(); int[] prepStmtUpdateCount = prepBatchStmt.executeBatch(); conn.commit(); |