Day 8

Quiz

A1:

The Statement interface is the parent of the PreparedStatement and CallableStatement interfaces. The Statement interface contains the basic methods for interacting with databases but it is not optimized for performance and it does not leverage database-specific functions. The PreparedStatement interface provides for precompiling SQL statements and storing them in the database, which results in better performance for SQL statements as compared to the Statement interface. The CallableStatement interface, on the other hand, enables you to execute database-specific programs such as functions and stored procedures from within a Java application.

A2:

To retrieve data from a ResultSet, the JDBC API provides a suite of getXXX() methods. The traversing of the ResultSet is always from the column number 1.

A3:

The JDBC API provides the execute(), executeQuery(), and executeUpdate() methods to execute SQL statements.

A4:

In order to make a ResultSet scrollable in both forward and reverse, set the type of the result set to either TYPE_SCROLL_SENSITIVE or TYPE_SCROLL_INSENSITIVE when you create the Statement object.

A5:

To make a ResultSet updatable, set the concurrency mode to CONCUR_UPDATABLE when you create the Statement object.

A6:

The JDBC 2.0 API provides the batch update facility that processes multiple SQL statements all at once. The Statement interface contains the addBatch(), executeBatch(), and clearBatch() methods to carry out batch updates.

A7:

BLOB, CLOB, ARRAY, STRUCT, REF, and DISTINCT are the SQL3 data types available in the JDBC 2.0 API.

A8:

The primary difference between a RowSet and a ResultSet is that a RowSet can be used in a connected or disconnected mode, that is, it may or may not require the use of a live database connection all the time. On the other hand, a ResultSet always requires a live database connection to perform its operations.

A9:

The three different types of RowSets available are CachedRowSet, JDBCRowSet, and WebRowSet.

Exercises

A1:

Add this bit of code in the try block of ProcessTransaction.jsp:

 //describing a new ResultSet to demonstrate use of Rowsets         Statement stmt1 = conn.createStatement();        stmt1.execute("select * from transaction_details");        ResultSet rowSetDemoRS = stmt1.getResultSet();        CachedRowSet myCacheRowSet = new CachedRowSetImpl();        myCacheRowSet.populate(rowSetDemoRS);        rowSetDemoRS.close(); stmt1.close();        while(myCacheRowSet.next()){               int rowSetTransId = myCacheRowSet.getInt("transaction_id");               String rowSetCustName =                       myCacheRowSet.getString("customer_name");               String rowSetTotalAmt =                       myCacheRowSet.getString("total_amount");               String rowSetCreditCard = myCacheRowSet.getString("credit                       card_no");               String rowSetCardType = myCacheRowSet.getString("card_type");               out.println("                       <BR><CENTER>** SAMPLE ROWSET IMPLEMENTATION  **</CENTER><BR>");               out.println("Transaction ID : "+rowSetTransId);               out.println("<BR>Customer Name : "+rowSetCustName);               out.println("<BR>Total Amount : "+rowSetTotalAmt);               out.println("<BR>Credit Card Type : "+rowSetCreditCard);               out.println("<BR>Card Type : "+rowSetCardType);        } 

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(); 



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net