Three methods for connecting Java JDBC Thin Driver to Oracle: jdbcthin

Source: Internet
Author: User

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

 

  1. Import java. SQL .*;
  2. Public class jdbc {
  3. String dbUrl = "jdbc: oracle: thin: @ 127.0.0.1: 1521: newccs ";
  4. String theUser = "dave ";
  5. String thePw = "dave ";
  6. Connection c = null;
  7. Statement conn;
  8. ResultSet rs = null;
  9. Public jdbc (){
  10. Try {
  11. Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
  12. C = DriverManager. getConnection (dbUrl, theUser, thePw );
  13. Conn = c. createStatement ();
  14. } Catch (Exception e ){
  15. E. printStackTrace ();
  16. }
  17. }
  18. Public boolean executeUpdate (String SQL ){
  19. Try {
  20. Conn.exe cuteUpdate (SQL );
  21. Return true;
  22. } Catch (SQLException e ){
  23. E. printStackTrace ();
  24. Return false;
  25. }
  26. }
  27. Public ResultSet executeQuery (String SQL ){
  28. Rs = null;
  29. Try {
  30. Rs = conn.exe cuteQuery (SQL );
  31. } Catch (SQLException e ){
  32. E. printStackTrace ();
  33. }
  34. Return rs;
  35. }
  36. Public void close (){
  37. Try {
  38. Conn. close ();
  39. C. close ();
  40. } Catch (Exception e ){
  41. E. printStackTrace ();
  42. }
  43. }
  44. Public static void main (String [] args ){
  45. ResultSet rs;
  46. Jdbc conn = new jdbc ();
  47. Rs = conn.exe cuteQuery ("select * from dave where rownum <5 ");
  48. Try {
  49. While (rs. next ()){
  50. System. out. println (rs. getString ("username") + "--" + rs. getString ("user_id "));
  51. }
  52. } Catch (Exception e ){
  53. E. printStackTrace ();
  54. }
  55. }
  56. }
  57. --- The output is normal:
  58. MGMT_VIEW--97
  59. ANQING--94
  60. DVD--93
  61. 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.

 

  1. Import java. SQL .*;
  2. Public class jdbc {
  3. String dbUrl = "jdbc: oracle: thin: @ dvd ";
  4. // String dbUrl = "jdbc: oracle: thin: @ // 127.0.0.1: 1521/dave ";
  5. // String dbUrl = "jdbc: oracle: thin: @ 127.0.0.1: 1521: newccs ";
  6. String theUser = "dave ";
  7. String thePw = "dave ";
  8. Connection c = null;
  9. Statement conn;
  10. ResultSet rs = null;
  11. Public jdbc (){
  12. Try {
  13. System. setProperty ("oracle.net. tns_admin", "D :\\ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN ");
  14. Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();
  15. // Class. forName ("oracle. jdbc. OracleDriver"). newInstance ();
  16. C = DriverManager. getConnection (dbUrl, theUser, thePw );
  17. Conn = c. createStatement ();
  18. } Catch (Exception e ){
  19. E. printStackTrace ();
  20. }
  21. }
  22. Public boolean executeUpdate (String SQL ){
  23. Try {
  24. Conn.exe cuteUpdate (SQL );
  25. Return true;
  26. } Catch (SQLException e ){
  27. E. printStackTrace ();
  28. Return false;
  29. }
  30. }
  31. Public ResultSet executeQuery (String SQL ){
  32. Rs = null;
  33. Try {
  34. Rs = conn.exe cuteQuery (SQL );
  35. } Catch (SQLException e ){
  36. E. printStackTrace ();
  37. }
  38. Return rs;
  39. }
  40. Public void close (){
  41. Try {
  42. Conn. close ();
  43. C. close ();
  44. } Catch (Exception e ){
  45. E. printStackTrace ();
  46. }
  47. }
  48. Public static void main (String [] args ){
  49. ResultSet rs;
  50. Jdbc conn = new jdbc ();
  51. Rs = conn.exe cuteQuery ("select * from dave where rownum <5 ");
  52. Try {
  53. While (rs. next ()){
  54. System. out. println (rs. getString ("username") + "--" + rs. getString ("user_id "));
  55. }
  56. } Catch (Exception e ){
  57. E. printStackTrace ();
  58. }
  59. }
  60. }

 

 

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 -------------------------------------------------------------------------------------------------------

Related Article

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.