java连接oracle远程数据库_java通过程序连接远程SqlServer数据库,并将远程数据库的数据同步到本地oracle...

偶然碰到这个需求,在各种百度下终于成功完成需求。第一次写博客,希望大家多多指教!

在开发过程中,我最开始的问题是如何用java程序连接远程数据库,为此百度了很久才成功连接上远程数据库。

而且需要需求是说不能在代码里写死远程数据库的地址,需要将数据源写在配置文件(db.properties)里。

db.properties内容如下:

driver=oracle.jdbc.OracleDriver

url=jdbc:oracle:thin:@localhost:1521:ORCL

user=tzsb

password=tzsb

driver1=com.microsoft.sqlserver.jdbc.SQLServerDriver

url1=jdbc:sqlserver://远程数据库地址:端口号;DatabaseName=数据库名称

user1=数据库用户名

password1=数据库密码

因为需要操作两个不同的数据库,所以我在配置文件里写好了远程数据库和本地数据库的数据源信息。

在此需要一个工具类来读取这个配置文件db.properties里面的数据源信息。这个工具类是ReadProperties。

ReadProperties.java内容如下:

package com.wintime.db;

import java.io.InputStream;

import java.util.Properties;

public class ReadProperties {

static private String driver = null;

static private String url = null;

static private String user = null;

static private String password = null;

//因为需要操作远程和本地两个数据库,所以需要定义两种变量,下面的get方法同理

static private String driver1 = null;

static private String url1 = null;

static private String user1 = null;

static private String password1 = null;

static {

loads();

}

synchronized static public void loads() {

if (driver == null || url == null || user == null || password == null) {

InputStream is = ReadProperties.class.getResourceAsStream("/db.properties");

Properties dbproperties = new Properties();

try {

dbproperties.load(is);

driver = dbproperties.getProperty("driver").toString();

url = dbproperties.getProperty("url").toString();

user = dbproperties.getProperty("user&

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值