【问题标题】:Failing to obtain a JDBC connection to Oracle [closed]无法获得到 Oracle 的 JDBC 连接 [关闭]
【发布时间】:2021-10-17 07:25:00
【问题描述】:

我目前正在尝试使用 JDBC 链接我的 Spring Boot 应用程序以连接到我的 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 都在运行。

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\18.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

对于我的项目构建:

  • Springboot 2.1.8 发布
  • Apache Tomcat v7.0
  • ojdbc8
  • Oracle-SQL-Developer 18.3.0.0
  • JDK 8

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 数据库连接:

  • 连接名称:XE
  • 身份验证类型:默认
  • 用户名:system
  • 密码:密码
  • 主机名:本地主机
  • 端口:1521
  • SID:xe

通过阅读类似的问题和答案,听起来我的 tnsnames/listener 文件中存在问题,但是在摆弄了两天的大部分时间后,我无法区分问题出在我的项目中。

【问题讨论】:

  • tnslistener 中的主机名似乎是您的笔记本电脑名称.. 可能是问题
  • @Sagii 您建议将主机名解析为什么?我刚试过'Host = localhost'并收到同样的错误。感谢回复
  • 你的 application.properties 有 locahost 而不是 localhost .. 拼写错误
  • @Sagii 谢谢!这已经向前推进了。不再收到该错误。

标签: java oracle spring-boot listener tnsnames


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 2018-09-09
    • 2016-10-28
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多