Bequeath Connection and SYS Logon

The following example illustrates how to use the internal_logon and SYSDBA arguments to specify the SYS login. This example works regardless of the database's national-language settings of the database.

/** Example of bequeath connection **/
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.*;

// create an OracleDataSource instance
OracleDataSource ods = new OracleDataSource();

// set neccessary properties
java.util.Properties prop = new java.util.Properties();
prop.put("user", "sys");
prop.put("password", "sys");
prop.put("internal_logon", "sysdba");
ods.setConnectionProperties(prop);

// the url for bequeath connection
String url = "jdbc:oracle:oci8:@";
ods.setURL(url);

// retrieve the connection
Connection conn = ods.getConnection();
... 

 
 
 
 
 
 
 
import oracle.jdbc.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;

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

public class OracleOCI {

        public static void main(String[] args) {
                try {
                        // Set the connection properties.
                        // required: SYSDBA
                        Properties prop = new Properties();
                        // prop.setProperty("user","sys");
                        // prop.setProperty("password","password");
                        prop.setProperty("internal_logon", "sysdba");
                        String sql = "select banner from v$version";
                        OracleDataSource ods = new OracleDataSource();
                        ods.setConnectionProperties(prop);
                        ods.setURL("jdbc:oracle:oci8:@");
                        OracleConnection conn = (OracleConnection) ods.getConnection();
                        System.out.println("Connected");

                        Statement stmt = conn.createStatement();

                        ResultSet rs = stmt.executeQuery(sql);

                        while (rs.next()) {
                                System.out.println(rs.getString("banner"));
                        }

                        rs.close();
                        stmt.close();
                        conn.close();

                } catch (SQLException e) {
                        System.out.println(e.getMessage());
                }

        }
}

  

posted @ 2015-09-05 05:01  syksky  阅读(436)  评论(0编辑  收藏  举报