How to connect in java as SYS to Oracle?
Asked Answered
W

9

33

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.

Worthen answered 11/4, 2012 at 7:15 Comment(2)
Please show the code which you use to connect.Bunni
When you say "in java", do you mean "via JDBC"?Squalene
R
42

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)
Recoup answered 11/4, 2012 at 18:4 Comment(4)
What is that, Groovy?Squalene
The question is asked in java.Coincident
whats the way in tomcat server.xml file?Diacritical
proper jdbc url for sys as sysdba: "jdbc:oracle:thin:sys as sysdba/[email protected]:1521/xe", tested with xe 11.2.0.2.0Darreldarrell
F
17

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'
Francophile answered 5/12, 2013 at 5:50 Comment(0)
U
8

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);
Unsung answered 11/4, 2012 at 17:56 Comment(0)
C
5

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
Censorship answered 11/4, 2012 at 7:29 Comment(1)
The question was about connecting via Java, which I presume means JDBC. The commands you gave are for use with SQL*Plus, if I'm not mistaken.Squalene
W
1

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");
Walleye answered 12/4, 2012 at 11:16 Comment(0)
M
1

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);
    }
Monger answered 10/6, 2017 at 10:54 Comment(0)
M
1
        /*It works for me*/
        /*also oci and thin is important if you want to connect with database which is installed in your clien (Computer) add oci if you want to install to server add thin*/

        String dbURL2 = "jdbc:oracle:oci:@172.16.24.123:1521:XE";
        String username = "sys as sysdba";
        String password = "XX Change it to your system password";
        
        try {
        Connection connection = DriverManager.getConnection(dbURL2, username, password);
            System.out.println("Connected to Oracle data");
        
    } catch (SQLException e) {
            System.out.println("Opps ! error");
            e.printStackTrace();
        
    }
Marcelinomarcell answered 23/11, 2020 at 22:29 Comment(1)
Please explain what your code does and how it does it.Wyandotte
O
0

You need to put sysdba with user String parameter like

String user="sys as sysdba"
Osmanli answered 11/1, 2018 at 12:36 Comment(0)
Y
0

My two cents as I ran into the same exception. Following did the trick for me:

           try (Connection conn = DriverManager.getConnection(
                "jdbc:oracle:thin:@myserverip:1521:XE", "sys as sysdba", "Hello123")) {   
           // this worked too - "jdbc:oracle:thin:@myserverip:1521:XE", "system"
              if (conn != null) {
                  System.out.println("Connected to the database!");
              } else {
                System.out.println("Failed to make connection!");
              }
 
            } catch (SQLException e) {
              System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
            } catch (Exception e) {
            e.printStackTrace();
           }
Yeta answered 8/12, 2020 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.