日期:2014-05-16  浏览次数:20431 次

JDBC连接Oracle 11g异常

最近的一个web应用,因为服务端是aix下新装的oracle11g,要求所有系统换成11g~

重新配置好ip地址后,以前的应用报错:

?

Listener refused the connection with the following error:ORA-12505, TNS:listener does not currently know of SID given in connect descriptor.

?

sid配置正确但无法被识别,于是网上查有如下结果(注意红字的一行)

?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author
*/
public class ConnectJDBCOracle11g {

/**
* This class demonstrates the code for connecting Oracle 11g database using JDBC.
* @param args
*/
public static void main(String[] args) {
String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
String JDBC_STRING = "jdbc:odbc:thin:@HOSTNAME:PORTNUMBER/SID"; // in case of 11g use '/' instead of :
String USER_NAME = "USER_NAME";
String PASSWD = "PASSWORD";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(JDBC_STRING, USER_NAME, PASSWD);
stmt = conn.createStatement();

String query = "SELECT * FROM TABLE TBL";
rs = stmt.executeQuery(query);
}catch(SQLException sqlEx){
sqlEx.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
try {
if(rs!=null) rs.close();
if(stmt !=null) stmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

?

原来在11g中,连接地址:jdbc:odbc:thin:@HOSTNAME:PORTNUMBER/SID ,SID前是斜杠,而不是冒号了.

问题解决

?