Following is the example, which makes use of the Result Set. TYPE_SCROLL_INSENSITIVE described in the Result Set tutorial.

updating a sql recordset-80

Copy and past the following example in JDBCExample.java, compile and run as follows − //STEP 1.

Import required packages import *; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.

Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; try{ //STEP 2: Register JDBC driver Name("com.

Driver"); //STEP 3: Open a connection println("Connecting to database..."); conn = Driver Connection(DB_URL, USER, PASS); //STEP 4: Execute a query to create statment with // required arguments for RS example.

"); }//end main public static void print Rs(Result Set rs) throws SQLException{ //Ensure we start with first row rs.before First(); while(rs.next()){ //Retrieve by column name int id = Int("id"); int age = Int("age"); String first = String("first"); String last = String("last"); //Display values print("ID: " + id); print(", Age: " + age); print(", First: " + first); println(", Last: " + last); } println(); }//end print Rs() }//end JDBCExample java JDBCExample Connecting to database... ID: 100, Age: 38, First: Zara, Last: Ali ID: 101, Age: 45, First: Mahnaz, Last: Fatma ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal Inserting a new record... ID: 100, Age: 38, First: Zara, Last: Ali ID: 101, Age: 45, First: Mahnaz, Last: Fatma ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal ID: 104, Age: 40, First: John, Last: Paul List the record before deleting...

ID: 101, Age: 45, First: Mahnaz, Last: Fatma List result set after deleting one records...

ID: 100, Age: 38, First: Zara, Last: Ali ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal ID: 104, Age: 40, First: John, Last: Paul Goodbye!

println("Creating statement..."); Statement stmt = conn.create Statement( Result Set. CONCUR_UPDATABLE); //STEP 5: Execute a query String sql = "SELECT id, first, last, age FROM Employees"; Result Set rs = stmt.execute Query(sql); println("List result set for reference...."); print Rs(rs); //STEP 6: Loop through result set and add 5 in age //Move to BFR postion so while-loop works properly rs.before First(); //STEP 7: Extract data from result set while(rs.next()){ //Retrieve by column name int new Age = Int("age") + 5; rs.update Double( "age", new Age ); rs.update Row(); } println("List result set showing new ages..."); print Rs(rs); // Insert a record into the table.

//Move to insert row and add column data with update XXX() println("Inserting a new record..."); To Insert Row(); rs.update Int("id",104); rs.update String("first","John"); rs.update String("last","Paul"); rs.update Int("age",40); //Commit row rs.insert Row(); println("List result set showing new set..."); print Rs(rs); // Delete second record from the table.