Wednesday, 1 October 2014

Check ResultSet type is supported by database or not

As already said, there are 3 types of ResultSet(TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, TYPE_SCROLL_SENSITIVE). 'java.sql.DatabaseMetaData' interface provides 'supportsResultSetType' method to check whether this database supports the given result set type or not.

/* 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();
                     
        if (dm.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)) {
            System.out.println("Forward-only");
        }
        
        if (dm.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
            System.out.println("Scroll-insensitive");
        }
        
        if (dm.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
            System.out.println("Scroll-sensitive");
        }
        
        conn.close();
    }
}

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment