未能获得与 Oracle 的 JDBC 连接

Failing to obtain a JDBC connection to Oracle

我目前正在尝试 link 我的 Spring 使用 JDBC 启动应用程序以连接到我的 Oracle 数据库。

项目的整体设计是为了能够进行CRUD操作。

当我编译 运行 我的项目时,最初没有出现任何问题。

当我尝试使用 application.properties 文件中的预定端口连接到浏览器中的本地主机时,我从浏览器收到以下错误消息:

There was an unexpected error (type=Internal Server Error, status=500).
Failed to obtain JDBC Connection; nested exception is 
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

我在 Eclipse 控制台中收到的(最小化的)错误消息如下:

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
     at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:801) ~[ojdbc8-18.3.0.0.jar:18.3.0.0.0]
     ...
     ...

Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
     at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:569) ~[ojdbc8-18.3.0.0.jar:18.3.0.0.0]
     ...
     ...

Caused by: java.net.UnknownHostException: locahost
    at java.net.InetAddress.getAllByName0(Unknown Source) ~[na:1.8.0_151]
     ...
     ...

2021-08-14 10:09:01.177 ERROR 33096 --- [nio-8095-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection] with root cause

java.net.UnknownHostException: locahost
     at java.net.InetAddress.getAllByName0(Unknown Source) ~[na:1.8.0_151]
     ...
     ...

我的 TNSListener 和我的 OracleServiceXE 都是 运行ning。

tnsnames.ora 文件

    XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = LAPTOP-R01ERMVG)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = XE)
    )
  )

LISTENER_XE =
  (ADDRESS = (PROTOCOL = TCP)(HOST = LAPTOP-R01ERMVG)(PORT = 1521))


ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

listener.ora 文件

 SID_LIST_LISTENER=
   (SID_LIST=
     (SID_DESC=

          (GLOBAL_DBNAME=salesdb.mycompany)

          (SID_NAME=XE)         
          (ORACLE_HOME=C:\app\bendg\product.0.0\dbhomeXE)
            #PRESPAWN CONFIG
         (PRESPAWN_MAX=20)
      (PRESPAWN_LIST=
           (PRESPAWN_DESC=(PROTOCOL=tcp)(POOL_SIZE=2)(TIMEOUT=1))
      )
     )
    )

我的 hosts 文件有一行说明:

127.0.0.1       localhost

对于我的项目构建:

application.properties 文件

spring.datasource.url=jdbc:oracle:thin:@locahost:1521:XE

spring.datasource.username=system
spring.datasource.password=password
logging.level.root=INFO
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

server.port=8095

pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.codejava</groupId>
  <artifactId>central</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
  </parent>
  
  <!-- Dependencies -->
  
  <dependencies> 

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
        
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
        
    <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>18.3.0.0</version>                         
    </dependency>
                
  </dependencies>
    
        <build>
            <plugins>
                <plugin> 
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>               
                </plugin>           
            </plugins>      
        </build>
          
</project>

对于 Oracle 数据库连接:

从阅读类似的问题和答案来看,我的 tnsnames/listener 文件中似乎存在问题,但是在摆弄了两天的大部分时间后,我无法区分问题出在哪里。

将您的 application.properties 更新到下面并检查这可能会解决您的连接问题

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE