Wednesday, 1 October 2014

Set ResultSet Holdability using Connection object

As already discussed there are two constants provides in java.sql.ResultSet interface to represent Result Set Holdability.
    a. CLOSE_CURSORS_AT_COMMIT
    b. HOLD_CURSORS_OVER_COMMIT

While creating the statement using Connection object, we can set the result set Holdability.

stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
Above statement creates a Statement object that will generate ResultSet objects with the type 'TYPE_FORWARD_ONLY' , concurrency 'CONCUR_READ_ONLY' and holdability 'HOLD_CURSORS_OVER_COMMIT'.

/* Step 1: Import sql package */
import java.sql.*;

public class SampleApp {
    
    /* Update username, password and driver details here */
    static Connection getConnection() throws ClassNotFoundException, SQLException{
         /* Step 2: Load Driver */
        System.out.println("Loading/Registering driver");  
       
        Class.forName("com.mysql.jdbc.Driver");
         
        /* Step 3: Open connection to database */
        System.out.println("Connecting to database");
        String url = "jdbc:mysql://localhost/world";
        String userName = "root";
        String pasword = "tiger";
        return DriverManager.getConnection(url, userName, pasword);
    }
    
    public static void main(String args[]) throws SQLException, ClassNotFoundException{      
        Connection conn = getConnection();
        DatabaseMetaData dm = conn.getMetaData();
        Statement stmt;
        stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        
        stmt.close();
        conn.close();
    }
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment