import java.sql.*;
import java.util.*;
import oracle.jdbc.*;

/**
 * This class is a sample that illustrates how to connect to a Oracle Database
 * with DBA privileges i.e as SYS user.
 * 
 */
public class ConnectAsSysSample  {

  public ConnectAsSysSample( ) throws SQLException {
    // Register Oracle Driver with DriverMananger
    DriverManager.registerDriver( new OracleDriver( ) );
  }

  public static void main( String[] args ) throws SQLException {
    ConnectAsSysSample sample = new ConnectAsSysSample( );
    sample.getUserRoles( );
  }

  
  // Use the Data dictonary to display the role for this user
  public void getUserRoles( ) throws SQLException {
    
    Connection conn = this.getConnection( );   
    
    Statement stmt = conn.createStatement( );
    
    ResultSet rset = stmt.executeQuery( " SELECT UserName, Granted_Role, Admin_Option FROM USER_ROLE_PRIVS " );
    
    System.out.println( "UserName          Granted Role        Admin Option ");
    
    while ( rset.next( ) )
      System.out.println( rset.getString( 1 ) + "       " + rset.getString( 2 ) + "    " + rset.getString( 3 ) );
    
    rset.close( );
    stmt.close( );
    conn.close( );
  }
  
  // Returns a connection to database
  public Connection getConnection( ) throws SQLException  {
  
    // Change the connection string based on your database 
    // jdbc:oracle:thin:@dbhostname:port:dbSID  
    String jdbcurl = "jdbc:oracle:thin:@localhost:1521:ias904";
    
    // Use properties to specify that you want to connect with DBA privileges
    Properties props = new Properties( );
    
    props.setProperty( OracleDriver.logon_as_internal_str , "sysdba" );
    props.setProperty( OracleDriver.user_string , "sys" );
    
    // Change the password to your database password
    props.setProperty( OracleDriver.password_string , "sys" );    
    
    return DriverManager.getConnection( jdbcurl, props );    
  }
  
}