Java-based JDBC connection Oracle 11g RELEASE2 Case Analysis

Source: Internet
Author: User
Tags stmt


The example in this article describes Java's approach to Oracle 11g RELEASE2 based on JDBC. Share to everyone for your reference. Specifically as follows:



The JDBC connection for Oracle 11g Release 2 appears to be different if you receive the following exception:



Listener refused the connection with the following error:ora-12505, Tns:listener does no currently know of SID given in C Onnect Descriptor.



Then you must use the following connection method:




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



I hope this article will help you with your Java programming.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.