Java開辟Oracle數據庫銜接JDBC Thin Driver 的三種辦法。本站提示廣大學習愛好者:(Java開辟Oracle數據庫銜接JDBC Thin Driver 的三種辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java開辟Oracle數據庫銜接JDBC Thin Driver 的三種辦法正文
Oracle的jdbc驅動三種重要分類:
1、JDBC OCI: oci是oracle call interface的縮寫,此驅動相似於傳統的ODBC 驅動。由於它須要Oracle Call Interface and Net8,所以它須要在運轉應用此驅動的JAVA法式的機械上裝置客戶端軟件,其實重要是用到orcale客戶端裡以dll方法供給的oci和辦事器設置裝備擺設。
2、JDBC Thin: thin是for thin client的意思,這類驅動普通用在運轉在WEB閱讀器中的JAVA法式。它不是經由過程OCI or Net8,而是經由過程Java sockets停止通訊,是純java完成的驅動,是以不須要在應用JDBC Thin的客戶端機械上裝置orcale客戶端軟件,所以有很好的移植性,平日用在web開辟中。
3、JDBC KPRB: 這類驅動由直接存儲在數據庫中的JAVA法式應用,如Java Stored Procedures 、triggers、Database JSP's。由於是在辦事器外部應用,他應用默許或以後的會話銜接來訪數據庫,不須要用戶名暗碼等,也不須要數據庫url。
一.JDBC 銜接Oracle 解釋
JDBC 的運用銜接Oracle 碰到成績,毛病以下:
ORA-12505,TNS:listener does not currently know of SID given in connect descriptor TheConnection descriptor used by the client was。
我在DB 層面設置裝備擺設了靜態注冊,而且GLOBAL_DBNAME和SID_NAME 紛歧樣,以往的設置裝備擺設都是一樣的,所以沒有發明這個成績。
(SID_DESC = (GLOBAL_DBNAME = dave) (ORACLE_HOME =D:\app\Administrator\product\11.2.0\dbhome_1) (SID_NAME = NEWCCS) )
Oracle Listener 靜態注冊 與 靜態注冊
http://www.jb51.net/article/75885.htm
在網上谷歌 了一下,發明JDBC Thin Driver 的formats 有三種格局:
格局一: Oracle JDBC Thin using a ServiceName:
jdbc:oracle:thin:@//<host>:<port>/<service_name>
Example: jdbc:oracle:thin:@//192.168.2.1:1521/XE
留意這裡的格局,@前面有//, 這是與應用SID的重要差別。
這類格局是Oracle 推舉的格局,由於關於集群來講,每一個節點的SID 是紛歧樣的,然則SERVICE_NAME 確可以包括一切節點。
格局二: Oracle JDBC Thin using an SID:
jdbc:oracle:thin:@<host>:<port>:<SID>
Example: jdbc:oracle:thin:192.168.2.1:1521:X01A
Note: Support for SID is being phased out. Oracle recommends that users switch over to usingservice names.
格局三:Oracle JDBC Thin using a TNSName:
jdbc:oracle:thin:@<TNSName>
Example: jdbc:oracle:thin:@GL
Note:
Support for TNSNames was added in the driver release 10.2.0.1
二.測試
2.1 預備任務:
Oracle 是11gR2
Listener.ora SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = dave) (ORACLE_HOME =D:\app\Administrator\product\11.2.0\dbhome_1) (SID_NAME = NEWCCS) ) ) Tnsnames.ora DVD = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = dave) ) )
2.2 測試1,應用SID:newccs
.imporjava.sql.* .publiclasjdb StrindbUr"jdbc:oracle:thin:@...::newccs" StrintheUse"dave" StrintheP"dave" Connectionull Statemenconn ResultSernull publijdbc( tr Class.forName("oracle.jdbc.driver.OracleDriver").newInstance() DriverManager.getConnection(dbUrltheUserthePw) conc.createStatement() catc(Exceptioe e.printStackTrace() publibooleaexecuteUpdate(Strinsql tr conn.executeUpdate(sql) returtrue catc(SQLExceptioe e.printStackTrace() returfalse publiResultSeexecuteQuery(Strinsql rnull tr rconn.executeQuery(sql) catc(SQLExceptioe e.printStackTrace() returrs publivoiclose( tr conn.close() c.close() catc(Exceptioe e.printStackTrace() publistativoimain(String[args ResultSers jdbconnejdbc() rconn.executeQuery("selecfrodavwherrownum<") tr whil(rs.next() System.out.println(rs.getString("username")+"--"+rs.getString("user_id")) catc(Exceptioe e.printStackTrace() . .---輸入正常 .MGMT_VIEW-- .ANQING-- .DVD-- .SYSMAN--
2.3 應用 service_name:dave
將2.2 節的dbUrl 改成以下:
String dbUrl = "jdbc:oracle:thin:@//127.0.0.1:1521/dave";
輸入成果:
MGMT_VIEW--97
ANQING--94
DVD--93
SYSMAN--95
假如在11g裡碰到以下毛病:
測試運轉Java 類,報錯:
java.sql.SQLException: The Network Adapter could not establish the connection
可以測驗考試改換對應的 jdbc connection driver,官網的解釋以下:
JDBC Thin Driver 11g Causes"Java.Sql.Sqlexception: Io Exception: The Network Adapter Could NotEstablish The Connection" While Connecting to Oracle Database 11g [ID947653.1]
Change the JDBC connection driver class inyour application server from:
oracle.jdbc.driver.OracleDriver
to
oracle.jdbc.OracleDriver
2.4 應用TNS name: dvd
String dbUrl = "jdbc:oracle:thin:@dvd";
報錯以下:
java.sql.SQLException: Unknown host specified
該成績是由於JVM 沒有oracle.net.tns_admin的system property。
處理辦法有2種:
辦法一:在啟動VM 時添加以下參數:
-Doracle.net.tns_admin=D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN
辦法二:在java 代碼裡添加:
System.setProperty("oracle.net.tns_admin","D:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\NETWORK\\ADMIN");
添加上後,便可以正常在JDBC中應用tnsnama了。
.imporjava.sql. .publiclasjd StrindbUr"jdbc:oracle:thin:@dvd .StrindbUr"jdbc:oracle:thin:@//...:/dave .StrindbUr"jdbc:oracle:thin:@...::newccs StrintheUse"dave StrintheP"dave Connectionul Statemencon ResultSernul publijdbc t System.setProperty("oracle.net.tns_admin","D:\\app\\Administrator\\product\\..\\dbhome_\\NETWORK\\ADMIN" Class.forName("oracle.jdbc.driver.OracleDriver").newInstance( ./Class.forName("oracle.jdbc.OracleDriver").newInstance( DriverManager.getConnection(dbUrltheUserthePw conc.createStatement( catc(Exceptio e.printStackTrace( publibooleaexecuteUpdate(Strinsq t conn.executeUpdate(sql returtru catc(SQLExceptio e.printStackTrace( returfals publiResultSeexecuteQuery(Strinsq rnul t rconn.executeQuery(sql catc(SQLExceptio e.printStackTrace( returr publivoiclose t conn.close( c.close( catc(Exceptio e.printStackTrace( publistativoimain(String[arg ResultSer jdbconnejdbc( rconn.executeQuery("selecfrodavwherrownum<" t whil(rs.next( System.out.println(rs.getString("username")+"--"+rs.getString("user_id") catc(Exceptio e.printStackTrace(
上面給年夜家分享一段代碼關於Java中經由過程jdbc(thin方法)銜接Oracle數據庫
package com.jdbc.OracleTest; import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class OracleJdbcConnectionTest { /** * by jarin 年代日 * @param args */ public static void main(String[] args) { try { //第一步要Jar包:在oracle裝置的目次下,盤符:\oracle\ora\jdbc\lib\ojdbc.jar //加載驅動 Class.forName("oracle.jdbc.driver.OracleDriver"); //銜接字符串(協定名:jdbc,子協定名: oracle:thin 子稱號:@localhost::oracleDB) String url ="jdbc:oracle:thin:@localhost::ora"; try { //樹立銜接 Connection conn = DriverManager.getConnection(url,"scott",""); //創立Statement或許是PreparedStatement語句 Statement st = conn.createStatement(); String sqlStr= "select ename from emp"; //履行查詢 ResultSet rs = st.executeQuery(sqlStr); //遍歷成果 while (rs.next()) { System.out.println(rs.getString()); } } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }