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:
- Package jdbc.dex.com;
- Import java. SQL. Connection;
- Import java. SQL. DriverManager;
- Import java. SQL. ResultSet;
- Import java. SQL. SQLException;
- Import java. SQL. Statement;
- Import org. junit. Test;
- Public class JDBC1 {
- Public static void main (String [] args ){
- String urlVip = "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)" + ")" + ")";
- String urlScan1 = "jdbc: oracle: thin: @ (DESCRIPTION =" +
- "(ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.100.217) (PORT = 1521)" +
- "(CONNECT_DATA =" +
- "(SERVER = DEDICATED)" +
- "(SERVICE_NAME = prod)" +
- ")" +
- ")";
- String urlScan2 = "jdbc: oracle: thin: @ 192.168.100.217: 1521/prod ";
- String driver = "oracle. jdbc. driver. OracleDriver ";
- Connection con;
- Statement st;
- ResultSet rs;
- Try {
- Class. forName (driver );
- Con = DriverManager. getConnection (urlVip, "dexter", "xiaojun ");
- St = con. createStatement ();
- Rs1_st.exe cuteQuery ("select 2 from dual ");
- While (rs. next ()){
- System. out. println (rs. getString (1 ));
- }
- } Catch (ClassNotFoundException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (SQLException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- }
- }