Connect JDBC to the Oracle 11gR2 Cluster Environment

Source: Internet
Author: User

Use the scan method of the new Oracle 11gR2 component (assuming that scan_vip is 192.168.100.217)
String url = "jdbc: oracle: thin: @ 192.168.100.217: 1521/prod"
You can also use the vip connection method before 11gr2.
String url = "jdbc: oracle: thin: @ (DESCRIPTION ="
+ "(ADDRESS_LIST ="
+ "(ADDRESS = (PROTOCOL = TCP) (HOST = el1) (PORT = 1521 ))"
+ "(ADDRESS = (PROTOCOL = TCP) (HOST = el2) (PORT = 1521 ))"
+ "(LOAD_BALANCE = yes )"
+ ")" + "(CONNECT_DATA =" + "(SERVER = DEDICATED )"
+ "(SERVICE_NAME = prod)" + ")" + ")";

Note: jdbc thin uses the vip connection before 11gr2. ip addresses cannot be directly used in the host. Otherwise, the NL Exception was generated error may occur. Aliases are required. And defined in the hosts file.

For example, in this example, the vip addresses of the two machines are 192.168.100.123 and 192.168.100.124, And the aliases are el1 and el2. The hosts file requires the following two lines:
192.168.100.123 el1
192.168.100.124 el2

Host file location:
Linux | Unix:/etc/hosts
Windows: $ System Disk \ WINDOWS \ system32 \ drivers \ etc \ hosts

The following is a complete example:

  1. Package jdbc.dex.com;
  2. Import java. SQL. Connection;
  3. Import java. SQL. DriverManager;
  4. Import java. SQL. ResultSet;
  5. Import java. SQL. SQLException;
  6. Import java. SQL. Statement;
  7. Import org. junit. Test;
  8. Public class JDBC1 {
  9. Public static void main (String [] args ){
  10. String urlVip = "jdbc: oracle: thin: @ (DESCRIPTION ="
  11. + "(ADDRESS_LIST ="
  12. + "(ADDRESS = (PROTOCOL = TCP) (HOST = el1) (PORT = 1521 ))"
  13. + "(ADDRESS = (PROTOCOL = TCP) (HOST = el2) (PORT = 1521 ))"
  14. + "(LOAD_BALANCE = yes )"
  15. + ")" + "(CONNECT_DATA =" + "(SERVER = DEDICATED )"
  16. + "(SERVICE_NAME = prod)" + ")" + ")";
  17. String urlScan1 = "jdbc: oracle: thin: @ (DESCRIPTION =" +
  18. "(ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.100.217) (PORT = 1521)" +
  19. "(CONNECT_DATA =" +
  20. "(SERVER = DEDICATED)" +
  21. "(SERVICE_NAME = prod)" +
  22. ")" +
  23. ")";
  24. String urlScan2 = "jdbc: oracle: thin: @ 192.168.100.217: 1521/prod ";
  25. String driver = "oracle. jdbc. driver. OracleDriver ";
  26. Connection con;
  27. Statement st;
  28. ResultSet rs;
  29. Try {
  30. Class. forName (driver );
  31. Con = DriverManager. getConnection (urlVip, "dexter", "xiaojun ");
  32. St = con. createStatement ();
  33. Rs1_st.exe cuteQuery ("select 2 from dual ");
  34. While (rs. next ()){
  35. System. out. println (rs. getString (1 ));
  36. }
  37. } Catch (ClassNotFoundException e ){
  38. // TODO Auto-generated catch block
  39. E. printStackTrace ();
  40. } Catch (SQLException e ){
  41. // TODO Auto-generated catch block
  42. E. printStackTrace ();
  43. }
  44. }
  45. }

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.