How to Make JDBC Connection to Oracle 11g Release 2

Oracle 11g Release 2 of the JDBC connection seems to be different, if you receive the following exception: Listener refused the connection with the following error: ORA-12505, TNS: listener does not currently know of SID given in connect descriptor. Then you must use the following connections:

/************************************************* *********************************
 * Created on Nov, 2011 Copyright (c) All Rights Reserved.
 ************************************************** *********************************/
 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:
	 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 ();

one comment

  1. madhu says:

    “jdbc: odbc: thin: @ HOSTNAME: PORTNUMBER / SID”;

    We can replace this statement with the best suitable sting as

    “jdbc: odbc: thin: //@ localhost:1521 / XE”;

    this will work for oracle 11g xe in a best way. the default SID for oracle 11g express edition is: XE.

    If not you can see the SID at your system service.

    open services in your computer, select OracleServiceXE . right click, select properties,

    in general tab look for “path to executable”. it will show you as…

    “c:oraclexeapporacleproduct11.2.0serverbinORACLE.EXE XE” the last word in this line is your SID.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.