如何使用 java 代码登录 Oracle 11g 中的“xe”连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11945255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I login to 'xe' connection in Oracle 11g with java code?
提问by sweet dreams
I want to make java code that does only this:
我想制作只执行此操作的java代码:
Login to the 'xe' connection in Oracle 11g database. That's all. How can do it ?
登录到 Oracle 11g 数据库中的“xe”连接。就这样。怎么办呢?
EDIT:
yes, i was using JDBC, but unable to login to that connection. My url is jdbc:oracle:thin:@//localhost:1521/xe
, username = sys
and password = 123456
for the xe or sys connection. Then why can't i login to that connection ?
编辑:是的,我正在使用 JDBC,但无法登录到该连接。我的网址jdbc:oracle:thin:@//localhost:1521/xe
,username = sys
并password = 123456
为XE或SYS连接。那为什么我不能登录到该连接?
EDIT:
编辑:
I am very sorry, I forgot to add that I see another error besides the 1st one, i.e.
非常抱歉,我忘了补充,除了第一个错误,我还看到了另一个错误,即
SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
This is followed by-
接下来是——
Exception in thread "main" java.lang.NullPointerException
回答by J?cob
If you want to connect as SYS you have to use sys as sysdba
如果要以 SYS 身份连接,则必须使用 sys as sysdba
So in your java code try like the following code
所以在你的java代码中尝试像下面的代码
String url = "jdbc:oracle:thin:@//localhost:1521:xe";
String username = "sys as sysdba";
String password = "123456";
Connection connection= DriverManager.getConnection(url, username, password);
Regards
问候
回答by Teh Hippo
Unfortunately, your answer is a bit too vague.
不幸的是,你的答案有点太模糊了。
- If you want to login to databases through Java, use JDBC.
- If you're wanting to automate the logging in of an external program, you've got the wrong language :)
- If you're wanting to verify the database is up, see the first point.
- 如果要通过Java 登录数据库,请使用JDBC。
- 如果您想自动登录外部程序,那么您使用的语言是错误的 :)
- 如果您想验证数据库是否已启动,请参阅第一点。
Overall, look to JDBC. Oracle's documentation of JDBC can be found here.
总的来说,看看JDBC。可以在此处找到Oracle 的 JDBC 文档。
回答by Sudipta Majumder
You should write the following code :
您应该编写以下代码:
private String driver="oracle.jdbc.driver.OracleDriver";
private String dbURL="jdbc:oracle:thin:@localhost:1521:XE";
private String dbUserName="sys as sysdba";
private String dbPassword="123456";
try{
Class.forName(driver);
con=DriverManager.getConnection(dbURL,dbUserName,dbPassword);
-----//Rest of your Code//-------
}catch(Exception e){
e.printStackTrace();
}