如何在java中连接SYS到Oracle?

mum43rcc  于 2023-06-22  发布在  Oracle
关注(0)|答案(9)|浏览(166)

我收到这个错误:

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

如何修复?(我需要成为SYS)。谢谢

9wbgstp7

9wbgstp71#

试试这个:

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)
tez616oj

tez616oj2#

答案已经在那里了,
您正在尝试以sys身份连接,但服务器允许
要么

sys as sysdba

sys as sysoper

只需将用户参数更改为上面的任意一个

user='sys as sysdba'

user='sys as sysoper'
toiithl6

toiithl63#

这段代码有效

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);
pgccezyw

pgccezyw4#

如果您尝试像这样连接到数据库:connect SYS/<password>您使用了不再有效的语法(Oracle 9i之后)。
请尝试按以下方式连接:

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

x33g5p2x5#

您是否能够使用OracleDataSource对象?

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;
    }
}

然后这样称呼它:

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

zd287kbt6#

如果你想连接你的数据库与用户以外的“sys”作为“sysdba”那么你必须改变驱动程序从“瘦”到“oci”使成功的连接.

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);
    }
dy2hfwbg

dy2hfwbg7#

/*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();
        
    }
rlcwz9us

rlcwz9us8#

您需要将sysdba与user String参数一起放置,如

String user="sys as sysdba"
anauzrmj

anauzrmj9#

我的两分钱,因为我遇到了同样的例外。以下为我做的把戏:

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();
           }

相关问题