Wednesday 1 October 2014

Get ResultSet Holdability

java.sql.DatabaseMetaData interface provides 'getResultSetHoldability' method which returns the database's default holdability for ResultSet objects.

/* 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();
        
        int holdability = dm.getResultSetHoldability();
        
        if(holdability == ResultSet.HOLD_CURSORS_OVER_COMMIT){
             System.out.println("Hold");
        }
        else if(holdability == ResultSet.CLOSE_CURSORS_AT_COMMIT){
            System.out.println("Close");
        }
        else{
            System.out.println("Error");
        }
        conn.close();
    }
}

Output
Loading/Registering driver
Connecting to database
Hold


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment