Wednesday 1 October 2014

Set ResultSet concurrency using Connection object

As already discussed there are two constants provides in java.sql.ResultSet interface to represent Result Set concurrency
a. CONCUR_READ_ONLY
b. CONCUR_UPDATABLE

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

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

/* 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 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        
        stmt.close();
        conn.close();
    }
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment