How to connect in java as SYS to Oracle?

后端 未结 8 1615
花落未央
花落未央 2020-12-08 07:15

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 SY

相关标签:
8条回答
  • 2020-12-08 07:50

    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);
        }
    
    0 讨论(0)
  • 2020-12-08 07:52

    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'
    
    0 讨论(0)
  • 2020-12-08 07:52

    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);
    
    0 讨论(0)
  • 2020-12-08 08:00

    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
    
    0 讨论(0)
  • 2020-12-08 08:01

    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");
    
    0 讨论(0)
  • 2020-12-08 08:06

    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)
    
    0 讨论(0)
提交回复
热议问题