Three methods for connecting Java JDBC Thin Driver to Oracle (reproduced), jdbcthin
1. JDBC connection to Oracle
An error occurred while connecting JDBC applications to Oracle:
ORA-12505, TNS: listener does not currently know of SID given in connect descriptor TheConnection descriptor used by the client was.
I configured static registration at the DB level, and GLOBAL_DBNAME and SID_NAME are different. The previous configuration is the same, so I did not find this problem.
(SID_DESC =
(GLOBAL_DBNAME = dave)
(ORACLE_HOME = D: \ app \ Administrator \ product \ 11.2.0 \ dbhome_1)
(SID_NAME = NEWCCS)
)
Dynamic Registration and static registration of Oracle Listener
Http://blog.csdn.net/tianlesoftware/article/details/5543166
After logging on to the Internet, I found that JDBC Thin Driver has three formats:
Format 1: Oracle JDBC Thin using a ServiceName:
Jdbc: oracle: thin: @ // Example: jdbc: oracle: thin: @ // 192.168.2.1: 1521/XE
Note that the format here is @ followed by //, which is the main difference from using SID.
This format is recommended by Oracle, because for clusters, the SID of each node is different, but SERVICE_NAME can indeed contain all nodes.
Format 2: 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.
Format 3: 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
Ii. Test
2.1 preparations:
Oracle is 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 Test 1, using SID: newccs
- Import java. SQL .*;
- Public class jdbc {
- String dbUrl = "jdbc: oracle: thin: @ 127.0.0.1: 1521: newccs ";
- String theUser = "dave ";
- String thePw = "dave ";
- Connection c = null;
- Statement conn;
- ResultSet rs = null;
- Public jdbc (){
- Try {
- Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
- C = DriverManager. getConnection (dbUrl, theUser, thePw );
- Conn = c. createStatement ();
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- Public boolean executeUpdate (String SQL ){
- Try {
- Conn.exe cuteUpdate (SQL );
- Return true;
- } Catch (SQLException e ){
- E. printStackTrace ();
- Return false;
- }
- }
- Public ResultSet executeQuery (String SQL ){
- Rs = null;
- Try {
- Rs = conn.exe cuteQuery (SQL );
- } Catch (SQLException e ){
- E. printStackTrace ();
- }
- Return rs;
- }
- Public void close (){
- Try {
- Conn. close ();
- C. close ();
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- Public static void main (String [] args ){
- ResultSet rs;
- Jdbc conn = new jdbc ();
- Rs = conn.exe cuteQuery ("select * from dave where rownum <5 ");
- Try {
- While (rs. next ()){
- System. out. println (rs. getString ("username") + "--" + rs. getString ("user_id "));
- }
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- }
- --- The output is normal:
- MGMT_VIEW--97
- ANQING--94
- DVD--93
- SYSMAN--95
2.3 use service_name: dave
Change the dbUrl in section 2.2 to the following:
String dbUrl = "jdbc: oracle: thin: @ // 127.0.0.1: 1521/dave ";
Output result:
MGMT_VIEW--97
ANQING--94
DVD--93
SYSMAN--95
If the following error occurs in 11 GB:
Test and run Java class, error:
Java. SQL. SQLException: The Network Adapter cocould not establish the connection
You can try to change the corresponding jdbc connection driver. The official website is described as follows:
JDBC Thin Driver 11g Causes "Java. SQL. Sqlexception: Io Exception: The Network Adapter cocould 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 use TNS name: dvd
String dbUrl = "jdbc: oracle: thin: @ dvd ";
The following error is reported:
Java. SQL. SQLException: Unknown host specified
This problem occurs because the JVM does not have the system property of oracle.net. tns_admin. There are two solutions:
Method 1: Add the following parameters when starting the VM:
-Doracle.net. tns_admin = D: \ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN
Method 2: Add the following content to the java code:
System. setProperty ("oracle.net. tns_admin", "D :\\ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN ");
After adding, you can use tnsnama in JDBC normally.
- Import java. SQL .*;
- Public class jdbc {
- String dbUrl = "jdbc: oracle: thin: @ dvd ";
- // String dbUrl = "jdbc: oracle: thin: @ // 127.0.0.1: 1521/dave ";
- // String dbUrl = "jdbc: oracle: thin: @ 127.0.0.1: 1521: newccs ";
- String theUser = "dave ";
- String thePw = "dave ";
- Connection c = null;
- Statement conn;
- ResultSet rs = null;
- Public jdbc (){
- Try {
- System. setProperty ("oracle.net. tns_admin", "D :\\ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN ");
- Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
- // Class. forName ("oracle. jdbc. OracleDriver"). newInstance ();
- C = DriverManager. getConnection (dbUrl, theUser, thePw );
- Conn = c. createStatement ();
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- Public boolean executeUpdate (String SQL ){
- Try {
- Conn.exe cuteUpdate (SQL );
- Return true;
- } Catch (SQLException e ){
- E. printStackTrace ();
- Return false;
- }
- }
- Public ResultSet executeQuery (String SQL ){
- Rs = null;
- Try {
- Rs = conn.exe cuteQuery (SQL );
- } Catch (SQLException e ){
- E. printStackTrace ();
- }
- Return rs;
- }
- Public void close (){
- Try {
- Conn. close ();
- C. close ();
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- Public static void main (String [] args ){
- ResultSet rs;
- Jdbc conn = new jdbc ();
- Rs = conn.exe cuteQuery ("select * from dave where rownum <5 ");
- Try {
- While (rs. next ()){
- System. out. println (rs. getString ("username") + "--" + rs. getString ("user_id "));
- }
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- }
Bytes -------------------------------------------------------------------------------------------------------
All rights reserved. reprinted articles are allowed, but source addresses must be indicated by links. Otherwise, the documents will be held legally responsible!
Blog: http://blog.csdn.net/tianlesoftware
WEAVER: http://weibo.com/tianlesoftware
Bytes -------------------------------------------------------------------------------------------------------