Oracle集群(RAC)11g

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_16094/article/details/81823132

这次的客户是双机访问操作数据库,一台实例名是orcl1,一台是orcl2。访问数据库服务名是orcl,之前我也搜了些资料,最后的想法是写了一个测试程序来测试JDBC链接ORACLE RAC的连接串。代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
public class RacTest {
 
	/**
	 * 测试RAC
	 * 
	 */
	public static void main(String[] args) throws ClassNotFoundException {
 
		String url;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
 
		Properties conProps = new Properties();
 
		conProps.put("user", "sys");
		conProps.put("password", "sys");
		conProps.put("internal_logon", "sysdba");
 
		// String username = "sys";
		// String password = "oracle";
 
		url = "jdbc:oracle:thin:@(description= (ADDRESS_LIST =";
		url += "(address=(protocol=tcp)(host=10.37.27.111)(port=1521))";
		url += "(address=(protocol=tcp)(host=10.37.27.112)(port=1521))";
		url += "(load_balance=yes))";
		url += "(connect_data =";
		url += "(server = dedicated)";
		url += "(service_name=orcl)";
		url += "(failover_mode =";
		url += "(type=session)";
		url += "(method=basic)";
		url += "(retries=5)";
		url += "(delay=15))";
		url += " ))";
 
		String sql = "select * from v$instance";
 
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url, conProps);
			// conn = DriverManager.getConnection(url, username, password);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				System.out.print(rs.getString("INSTANCE_NAME"));
			}
			rs.close();
			rs = null;
			stmt.close();
			stmt = null;
			conn.close();
			conn = null;
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
 
			if (rs != null) {
				try {
					rs.close();
 
					rs = null;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
					stmt = null;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
					conn = null;
				} catch (SQLException ex) {
					ex.printStackTrace();
				}
			}
 
		}
 
	}
 
}

 

展开阅读全文

没有更多推荐了,返回首页