背景
今日收到通知,由于数据库服务器已经运行5年了,需要对数据库服务器进行升级,早期的应用是使用ip:port直接链接数据库的。本次的升级是更换这个数据库集群的机器,机器的IP也会发生变化,为了应对之后可能发生的数据库升级事情,计划使用域名链接数据库,在使用域名之前,尚不得知oracle数据库是否支持因此做了一下测试
环境准备
我是在本地环境做的测试 第一步修改hosts文件内容
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
10.0.254.154 www.test-oracle.com
新增一行解析记录
10.0.254.154 www.test-oracle.com
这里修改之后然后在测试一下域名解析配置是否生效
C:\Users\pc>ping www.test-oracle.com
正在 Ping www.test-oracle.com [10.0.254.154] 具有 32 字节的数据:
来自 10.0.254.154 的回复: 字节=32 时间=38ms TTL=62
来自 10.0.254.154 的回复: 字节=32 时间=36ms TTL=62
10.0.254.154 的 Ping 统计信息:
数据包: 已发送 = 2,已接收 = 2,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 36ms,最长 = 38ms,平均 = 37ms
这里看到也生效了 接下来编写java代码使用jdbc链接数据库,并执行查询
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url="jdbc:oracle:thin:@www.test-oracle.com:1521/orcl";
String user="t_order";
String password="gweltGfkjwHrg3ewt&";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(url, user, password);
Statement createStatement = connection.createStatement();
String sql = "select count(1) from t_order";
ResultSet executeQuery = createStatement.executeQuery(sql);
while (executeQuery.next()) {
int count = executeQuery.getInt(1);
System.out.println("订单总记录数为:" +count );
}
}
}
控制台输出结果为
到此测试完毕,域名链接oracle数据库可用