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

1 篇文章 0 订阅

转自:http://jamesq.bokee.com/1263981.html


经过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 Statement...n");
            stmt = con.createStatement ();

            System.out.print("  Opening ResultsSet...n");
            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 ResultSet...n");
            rset.close();

            System.out.print("  Closing Statement...n");
            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }



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

        try {
            System.out.print("  Closing Connection...n");
            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();

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值