In the last section, we have seen the ResultSet moving only in forward direction. In this section, we learn some advance features of ResultSets. If some properties are set while creating the ResultSet then we can randomly fetch the data and even update it witout any update queries.


The next example demonstrates the advance features of the ResultSet.


 


package com.visualbuilder;


 


import java.sql.Connection;


import java.sql.DriverManager;


import java.sql.ResultSet;


import java.sql.Statement;


 


public class UpdateableAndScrollableRS {


 


        public static void main(String[] args) {


 


            try {


                /** Loading the driver*/


                    Class.forName("com.mysql.jdbc.Driver");


                /** Getting Connection*/


                                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");


                /** Creating Statement*/


                                        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);


                /** Getting ResultSet*/


                                        ResultSet rs=stmt.executeQuery("select * from visualbuilder");


                    while(rs.next()) {


                                                System.out.print("Id is " + rs.getInt("id"));


                                                System.out.println(" Name is "+rs.getString("name"));


                                        }


                                        rs.absolute(1);


                                        rs.updateString(2,"Visual Builder");


                                        rs.updateRow();


                                        rs.beforeFirst();


                                        System.out.println("After Updation");


                            while(rs.next()) {


                                                System.out.print("Id is " + rs.getInt("id"));


                                                System.out.println(" Name is "+rs.getString("name"));


                                        }


                /** Closing the Connection*/


                                        stmt.close();


                                        con.close();


                                } catch (Exception e) {


                                        e.printStackTrace();


                                }


                }


}


 


Output:-


Id is 1 Name is Sun java


Id is 2 Name is Visual Builder


Id is 3 Name is Eclipse


Id is 4 Name is IBM


Id is 5 Name is Jakarta


After Updation


Id is 1 Name is Visual Builder


Id is 2 Name is Visual Builder


Id is 3 Name is Eclipse


Id is 4 Name is IBM


Id is 5 Name is Jakarta



                    

Copyright © 2013 VisualBuilder. All rights reserved