JDBC连接Oracle报错ORA-12505

xiaoxiao2021-02-28  12

时间:2017-12-19

异常:ORA-12505, TNS: listener does not currently know of SID given in connect descriptor

情景:写了一个Strust2 Demo连接Oracle数据库运行时报错,对应所用Jar为:ojdbc6.jar

原因:jdbc连接URL填写错误,

原本的写法是:jdbc:oracle:thin:@172.31.60.51:1521:SHRMS;

正确的写法是:

jdbc:oracle:thin:@(description=(address=(protocol=tcp)(port=1521)(host=172.31.60.51)(PORT = 1521))(connect_data=(SERVER = DEDICATED)(service_name=SHRMS)))

解决办法:参考这里

连接代码:

package com.qiu.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestOracle { public void testOracle() { Connection con = null;// 创建一个数据库连接 PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement ResultSet result = null;// 创建一个结果集对象 try { Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("开始尝试连接数据库!"); String url = "jdbc:oracle:thin:@(description=(address=(protocol=tcp)(port=1521)(host=172.31.60.51)(PORT = 1521))(connect_data=(SERVER = DEDICATED)(service_name=SHRMS)))"; //String url = "jdbc:oracle:thin:@172.31.60.51:1521:SHRMS;"; String user = "admin"; String password = "password"; con = DriverManager.getConnection(url, user, password); System.out.println("连接成功!"); }catch (Exception e){ e.printStackTrace(); } finally{ try{ if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); System.out.println("数据库连接已关闭!"); }catch (Exception e){ e.printStackTrace(); } } } }

转载请注明原文地址: https://www.6miu.com/read-2000293.html

最新回复(0)