萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> java基於JDBC連接Oracle 11g Release2實例分析

java基於JDBC連接Oracle 11g Release2實例分析

   本文實例講述了java基於JDBC連接Oracle 11g Release2的方法。分享給大家供大家參考。具體如下:

  Oracle 11g Release 2 的 JDBC 連接似乎有所不同 ,如果你收到下面的異常:

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

  那麼你必須用下面的連接方式:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /******************************************************* * 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(); } } } }

  希望本文所述對大家的java程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved