How to connect in java as SYS to Oracle?

2019-01-22 02:07发布

问题:

I receive this error:

java.sql.SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER

How to fix? (I need to be SYS). Thanks.

回答1:

try this :

import java.sql as jsql
import java.lang as lang
driver, url, user, passwd = (
"oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@localhost:1234:xxx1",
"sys as sysdba",
"xxx1")
 lang.Class.forName(driver)
 c = jsql.DriverManager.getConnection(url,user,passwd)


回答2:

This code works

String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName).newInstance();
String nameForConnect = "sys as sysdba";
String pass = "password";
String url = "jdbc:oracle:thin:@192.168.0.1:1521:ORCL";
Connection conn = DriverManager.getConnection(url, nameForConnect, pass);


回答3:

Answers already there,

you are trying to connect as sys but the Server allows

either sys as sysdba

or

sys as sysoper

just change user parameter as either one from above

user='sys as sysdba'

or

user='sys as sysoper'


回答4:

If you have attempted to connect to the database like this: connect SYS/<password> you have used a syntax that is no longer valid (After Oracle 9i).

Instead try to connect as the following:

connect SYS/<password> as SYSDBA or connect SYS/<password> as SYSOPER


回答5:

Are you able to use an OracleDataSource Object?

public class Database {    
    static OracleDataSource ods;    
    public static Connection openConnection(String URL, String user, String password,     String option) throws SQLException
    {
            Connection conn = null;
            Properties properties = new Properties();
            properties.put("user", user);
            properties.put("password", password);

            ods = new OracleDataSource();
            ods.setURL(URL);

            if(option != null)
            {
                properties.put("internal_logon", option);
            }

            ods.setConnectionProperties(properties);
            conn = ods.getConnection();

            return conn;
    }
}

And call it like this:

Connection con = null;    
con = Database.openConnection("YourJDBCConnectionURL", "YourSYSUser", "YourSYSPassword", "sysdba");


回答6:

If you want to connect your database with user other than "sys" as "sysdba" then you have to change the driver from "thin" to "oci" to make the successful connection.

try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String DB_URL="jdbc:oracle:oci:@localhost:1521:orcl";
        OracleDataSource ds1=new OracleDataSource();
        Properties prop1 = new Properties();
        prop1.setProperty("user","ravi");
        prop1.setProperty("password","******");
        prop1.setProperty("internal_logon","sysdba");
        ds1.setConnectionProperties(prop1);
        ds1.setURL(DB_URL);
        OracleConnection conn1 = (OracleConnection)ds1.getConnection();
        Statement stmt = conn1.createStatement();
        ResultSet rs = stmt.executeQuery("select * from dba_users");
        while (rs.next())
            System.out.println(rs.getString(1));
        conn1.close();
    } catch (Exception e) {
        System.out.println(e);
    }


回答7:

You need to put sysdba with user String parameter like

String user="sys as sysdba"