Java Examples - Use row methods

Advertisements


Problem Description:

How to use different row methods to get no of rows in a table, update table etc?

Solution:

Following example uses first, last,deletRow,getRow,insertRow methods of ResultSet to delete or inser a Row & move pointer of ResultSet to first or last Record.

import java.sql.*;

public class jdbcConn {
   public static void main(String[] args) throws Exception{
      Class.forName("org.apache.derby.jdbc.ClientDriver");
      Connection con = DriverManager.getConnection
      ("jdbc:derby://localhost:1527/testDb","name","pass");
      Statement stmt = con.createStatement
      (ResultSet.TYPE_SCROLL_SENSITIVE, 
      ResultSet.CONCUR_UPDATABLE);
      String query = "select * from emp";
      ResultSet rs = stmt.executeQuery(query);
      rs.last();
      System.out.println("No of rows in table="+rs.getRow());
      rs.moveToInsertRow();
      rs.updateInt("id", 9);
      rs.updateString("name","sujay");
      rs.updateString("job", "trainee");
      rs.insertRow();
      System.out.println("Row added");
      rs.first();
      rs.deleteRow();
      System.out.println("first row deleted");
   }
}

Result:

The above code sample will produce the following result.For this code to compile your database has to be updatable. The result may vary.

No of rows in table=5
Row added
first row deleted


Advertisements
Advertisements