The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: JDBC(TM) Database Access
Lesson: New Features in the JDBC 2.0 API

## Code Sample for Inserting a Row

> 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. At the time of this writing there are not yet any JDBC 2.0 Compliant drivers, so this code, though it compiles, has not been tested on a driver and DBMS.

> Here are some things you might notice about the code:

> 1. The `ResultSet` object `uprs` is updatable, scrollable, and sensitive to changes made by itself and others. Even though it is `TYPE_SCROLL_SENSITIVE` , it is possible that the `getXXX` methods called after the insertions will not retrieve values for the newly-inserted rows. There are methods in the `DatabaseMetaData` 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 same `ResultSet` 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 method `next` in a while loop. To be absolutely sure that the `getXXX` 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 same `Statement` object `stmt` and again using the query `SELECT * FROM COFFEES` .
> 2. 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 method `insertRow` . Then, still staying on the insert row, it sets the values for another row. **reduce to code snip and place rest in APP?**

> >     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