如何以 SYS 身份在 Java 中连接到 Oracle?
Posted
技术标签:
【中文标题】如何以 SYS 身份在 Java 中连接到 Oracle?【英文标题】:How to connect in java as SYS to Oracle? 【发布时间】:2012-04-23 11:55:45 【问题描述】:我收到此错误:
java.sql.SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
如何解决? (我需要SYS
)。
谢谢。
【问题讨论】:
请出示您用于连接的代码。 当你说“in java”时,你的意思是“via JDBC”吗? 【参考方案1】:试试这个:
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)
【讨论】:
那是什么,Groovy? 这个问题是用java问的。 tomcat server.xml 文件中的方式是什么? 正确的 jdbc url for sys as sysdba: "jdbc:oracle:thin:sys as sysdba/mysecretpassword@127.0.0.1:1521/xe",用 xe 11.2.0.2.0 测试【参考方案2】:答案已经存在,
您尝试以sys
身份连接,但服务器允许
要么
sys as sysdba
或
sys as sysoper
只需将用户参数更改为上面的任一参数
user='sys as sysdba'
或
user='sys as sysoper'
【讨论】:
感谢这个建议派上用场【参考方案3】:此代码有效
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);
【讨论】:
【参考方案4】:如果您尝试像这样连接到数据库:connect SYS/<password>
您使用的语法不再有效(Oracle 9i 之后)。
改为尝试如下连接:
connect SYS/<password> as SYSDBA or connect SYS/<password> as SYSOPER
【讨论】:
问题是关于通过 Java 连接的,我认为这意味着 JDBC。如果我没记错的话,您给出的命令是用于 SQL*Plus 的。【参考方案5】:您可以使用 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");
【讨论】:
【参考方案6】:如果您想将数据库与“sys”以外的用户连接为“sysdba”,则必须将驱动程序从“thin”更改为“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);
【讨论】:
【参考方案7】: /*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();
【讨论】:
请解释你的代码是做什么的以及它是怎么做的。【参考方案8】:你需要把sysdba和用户字符串参数一样
String user="sys as sysdba"
【讨论】:
【参考方案9】:当我遇到同样的异常时,我的两分钱。以下对我有用:
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();
【讨论】:
以上是关于如何以 SYS 身份在 Java 中连接到 Oracle?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 sys 和 sysdba 正确连接到 oracle?