`
bigfirebird
  • 浏览: 129470 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

oracle的sys用户怎么通过jdbc连接

阅读更多
oracle的sys用户怎么通过jdbc连接

经过GOOGLE 总算知道了如何在JDBC中使用SYSDBA角色连接ORACLE数据库

在DriverManager.getConnection通过Properties 设置连接的defaultRowPrefetch,nternal_logon 等信息。




oracle的sys用户怎么通过jdbc连接

经过GOOGLE 总算知道了如何在JDBC中使用SYSDBA角色连接ORACLE数据库

在DriverManager.getConnection通过Properties 设置连接的defaultRowPrefetch,nternal_logon 等信息。



// -----------------------------------------------------------------------------
// ConnectionOptions.java
// -----------------------------------------------------------------------------

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;


/**
* -----------------------------------------------------------------------------
* The following class provides an example of using jdbc to connect to an
* Oracle database using several advanced options. For example, one of the most
* used option is connecting to a database as SYSDBA. The following table
* contains
*
* Connection Properties Recognized by Oracle jdbc Drivers
* -------------------------------------------------------
* Name                Short Name Type     Description 
* ------------------- ---------- -------- -----------------------------------
* user                n/a        String   The user name for logging into the
*                                         database.
* password            n/a        String   The password for logging into the
*                                         database.
* database            server     String   The connect string for the database.
* internal_logon      n/a        String   A role, such as SYSDBA or SYSOPER,
*                                         that allows you to log on as SYS.
* defaultRowPrefetch  prefetch   String   (containing integer value) 
*                                         The default number of rows to
*                                         prefetch from the server.
*                                         (default value is "10") 
* remarksReporting    remarks    String   (containing boolean value) 
*                                         "true" if getTables() and
*                                         getColumns() should report
*                                         TABLE_REMARKS; equivalent to using
*                                         setRemarksReporting().
*                                         (default value is "false") 
* defaultBatchValue   batchvalue String   (containing integer value) 
*                                         The default batch value that triggers
*                                         an execution request.
*                                         (default value is "10") 
* includeSynonyms     synonyms   String   (containing boolean value)
*                                         "true" to include column information
*                                         from predefined "synonym" SQL
*                                         entities when you execute a
*                                         DataBaseMetaData getColumns() call;
*                                         equivalent to connection
*                                         setIncludeSynonyms() call.
*                                         (default value is "false") 

* -----------------------------------------------------------------------------
*/

public class ConnectionOptions {

    final static String driverClass    = "oracle.jdbc.driver.OracleDriver";
    final static String connectionURL  = "jdbc:oracle:thin:@localhost:1521:TRUESRC";
    final static String userID         = "scott";
    final static String userPassword   = "tiger";
    Connection   con                   = null;


    /**
     * Construct a QueryExample object. This constructor will create an Oracle
     * database connection.
     */
    public ConnectionOptions() {

        Properties conProps = new Properties();
        conProps.put("user", userID);
        conProps.put("password", userPassword);
        conProps.put("defaultRowPrefetch", "15");
        conProps.put("internal_logon", "sysdba");

        try {

            System.out.print("  Loading jdbc Driver  -> " + driverClass + "n");
            Class.forName(driverClass).newInstance();

            System.out.print("  Connecting to        -> " + connectionURL + "n");
            this.con = DriverManager.getConnection(connectionURL, conProps);
            System.out.print("  Connected as         -> " + userID + "n");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (InstantiationException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (SQLException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }


    /**
     * Method to check which database user we are logged in as
     */
    public void performUserQuery() {

        Statement stmt      = null;
        ResultSet rset      = null;
        String queryString  = "SELECT user, TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS') " +
                              "FROM   dual";

        try {

            System.out.print("  Creating Statementn");
            stmt = con.createStatement ();

            System.out.print("  Opening ResultsSetn");
            rset = stmt.executeQuery(queryString);

            rset.next();
            System.out.println("      Database User  -> " + rset.getString(1));
            System.out.println("      Date / Time    -> " + rset.getString(2));

            System.out.println();
            System.out.print("  Closing ResultSetn");
            rset.close();

            System.out.print("  Closing Statementn");
            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }



    /**
     * Close down Oracle connection.
     */
    public void closeConnection() {

        try {
            System.out.print("  Closing Connectionn");
            con.close();
           
        } catch (SQLException e) {
       
            e.printStackTrace();
           
        }

    }


    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     */
    public static void main(String[] args) {

        ConnectionOptions co = new ConnectionOptions();
        co.performUserQuery();
        co.closeConnection();

    }

}
分享到:
评论
1 楼 yangtaoxf126.com 2011-07-14  
public class SimpleJDBC {
public static void main(String args[]) {
try{
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Class.forName("oracle.jdbc.driver.OracleDriver");  
Properties props= new Properties();
props.put("user","SYS");
props.put("password","wwww");
props.put("internal_logon","sysdba");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl",props);
Statement stmt=conn.createStatement();
stmt.addBatch("grant select on sys.dba_pending_transactions to sys_0000000623 ");
stmt.addBatch("grant select on sys.pending_trans$ to sys_0000000623  ");
stmt.addBatch("grant select on sys.dba_2pc_pending to sys_0000000623 ");
stmt.addBatch("grant execute on sys.dbms_system to sys_0000000623 ");
stmt.executeBatch();

ResultSet rset=stmt.executeQuery("select username from x$kzsrt");
while(rset.next()){
System.out.println("username========= "+rset.getString(1));
}
rset=stmt.executeQuery("select * from dba_tab_privs where grantee = 'SYS_0000000623'");
while(rset.next()){
System.out.println("username========= "+rset.getString(3));
}
}catch(SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
}

相关推荐

Global site tag (gtag.js) - Google Analytics