Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following code sample is a complete program that should run if you have a JDBC 2.0 Compliant driver that implements scrollable result sets.
Note: Tutorial reader Nedzad Hodzic reported that in this example, you cannot useSELECT * FROM TAB_NAME
. You must name the columns; otherwise theResultSet
is not updateable. This is a bug he encountered when using an Oracle 8.1.6 database with Oracle driver OCI8.Here are some things you might notice about the code:
- The
ResultSet
objectuprs
is updatable, scrollable, and sensitive to changes made by itself and others. Even though it isTYPE_SCROLL_SENSITIVE
, it is possible that thegetXXX
methods called after the insertions will not retrieve values for the newly-inserted rows. There are methods in theDatabaseMetaData
interface that will tell you what is visible and what is detected in the different types of result sets for your driver and DBMS. These methods are discussed in detail in JDBC Database Access with Java, but they are beyond the scope of this tutorial. In this code sample we wanted to demonstrate cursor movement in the sameResultSet
object, so after moving to the insert row and inserting two rows, the code moves the cursor back to the result set, going to the position before the first row. This puts the cursor in position to iterate through the entire result set using the methodnext
in a while loop. To be absolutely sure that thegetXXX
methods include the inserted row values no matter what driver and DBMS is used, you can close the result set and create another one, reusing the sameStatement
objectstmt
and again using the querySELECT * FROM COFFEES
.- After all the values for a row have been set with
updateXXX
methods, the code inserts the row into the result set and the database with the methodinsertRow
. Then, still staying on the insert row, it sets the values for another row.import java.sql.*; public class InsertRows { public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; Statement stmt; try { Class.forName("myDriver.ClassName"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); uprs.moveToInsertRow(); uprs.updateString("COF_NAME", "Kona"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 10.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.updateString("COF_NAME", "Kona_Decaf"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 11.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.beforeFirst(); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String name = uprs.getString("COF_NAME"); int id = uprs.getInt("SUP_ID"); float price = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int total = uprs.getInt("TOTAL"); System.out.print(name + " " + id + " " + price); System.out.println(" " + sales + " " + total); } uprs.close(); stmt.close(); con.close(); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.