Wednesday, 1 October 2014

Get ResultSet Concurrency

java.sql.ResultSet interface provides 'getConcurrency' method, which returns the concurrency mode of this ResultSet object.

/* 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();
          /* Create table employee */
        String query = "CREATE TABLE employee (id int, name varchar(30), PRIMARY KEY(id))";
        Statement stmt = conn.createStatement();
        stmt.execute(query);     
        
        query = "SELECT * FROM employee";
        ResultSet rs = stmt.executeQuery(query);
        int type = rs.getConcurrency();
             
        if (type == ResultSet.CONCUR_READ_ONLY) {
            System.out.println("Read-only");
        }
        else if (type == ResultSet.CONCUR_UPDATABLE) {
            System.out.println("updatable");
        }
        else {
            System.out.println("Error");
        }
        
        System.out.println("Dropping table employee");
        query = "DROP TABLE employee";
        stmt.execute(query);
        
 rs.close();
        stmt.close();
        conn.close();
    }
}

Output
Loading/Registering driver
Connecting to database
Read-only
Dropping table employee



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment