java - jdbc 连接到 oracle 10 XE

标签 java jdbc oracle10g

<分区>

Possible Duplicate:
Java Oracle localhost connection error (ORA-12505)

当我尝试连接到 Oracle 10g Express Edition 时,出现错误:

java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:xe

我的示例代码:

    Class.forName("oracle.jdbc.OracleDriver");
    Connection connection = null;
    connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "mk119", "mk119");
    Statement stmt = connection.createStatement();

在数据库中:

SQL> connect mk119
Enter password:
Connected.
SQL> ed
Wrote file afiedt.buf

  1* select sys_context('userenv', 'instance_name') from dual
SQL> /

SYS_CONTEXT('USERENV','INSTANCE_NAME')
----------------------------------------------------------------------

xe

SQL>

请帮我解决这个问题。

编辑:堆栈跟踪:

java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:xe

        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
        at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
        at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
        at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
        at java.sql.DriverManager.getConnection(DriverManager.java:579)
        at java.sql.DriverManager.getConnection(DriverManager.java:221)
        at fingerprint.DatabaseTest.main(DatabaseTest.java:14)

编辑:完整代码:

import java.sql.*;

public class DatabaseTest {
    public static void main(String [] args){
        try{
            Class.forName("oracle.jdbc.OracleDriver");
            Connection connection = null;
            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "mk119", "mk119");
            Statement stmt = connection.createStatement();
            System.out.println("Connection created");
            stmt.close();
            connection.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

最佳答案

根据提供的答案here ,检查以下内容(答案来自相关的、可能重复的问题):

"检查\admin\NETWORK目录下的listener.ora文件是否有如下值:

XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

因此请检查您的连接 url:主机、端口、sid,以确保其设置正确。

关于java - jdbc 连接到 oracle 10 XE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14074644/

相关文章:

java - 处理一次获取两个相邻元素的集合(使用流)

java - FutureTask.get的CancellationException异常后访问Callable

java - com.microsoft.sqlserver.jdbc.SQLServerException : Incorrect syntax near '@P0'

spring - 为什么 Spring Boot 打开很多连接?

java - java中无需编码即可快速将CLOB转换为String

entity-framework - Entity Framework - 在 Oracle 10g 中删除实体 { 由于对象的当前状态,操作无效}

java - JLabel.setVisible(false) 使其他组件的位置改变

Java Lucene NGramTokenizer

oracle - 在 oracle 10g 中禁用特定时间的表单按钮

sql - 合并语句数组作为源表