Java analysis of Oracle 11g Release2 instance based on JDBC connection

  • 2020-04-01 03:56:38
  • OfStack

This article illustrates a Java method to connect to Oracle 11g Release2 based on JDBC. Share with you for your reference. The details are as follows:

The JDBC connection for Oracle 11g Release 2 seems to be different if you receive the following exception:
Listener 1999 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 connection:


/*******************************************************
* Created on Nov, 2011 Copyright(c) http://vigilance.co.in All Rights Reserved.
********************************************************/
package com.vigilance.java.sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * @author http://vigilance.co.in
 */
public class ConnectJDBCOracle11g {

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();
    }
  }
}
}

I hope this article has been helpful to your Java programming.


Related articles: