java 使用jdbc连接Greenplum数据库和Postgresql数据库

由于使用maven仓库,不能下载Greenplum的jar包,但是可以下载Postgresql的jar包,所以Greenplum的jar包,自己可以百度自行下载。名字就叫做greenplum.jar。
maven依赖如下所示:

<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>

2、db.properties配置文件里面如下所示:

# 1.posgresql
posgresql_driver=org.postgresql.Driver
posgresql_url=jdbc:postgresql://192.168.xx.xx:5432/数据库名称(即schema)
posgresql_user=账号
posgresql_password=密码

# 2.greenplum
greenplum_driver=com.pivotal.jdbc.GreenplumDriver
greenplum_url=jdbc:pivotal:greenplum://192.168.xx.xx:5432;DatabaseName=数据库名称(即schema)
greenplum_user=账号
greenplum_password=密码

3、然后连接Greenplum数据库和Postgresql数据库如下所示:

public class JdbcUtils {
// 1、Postgresql
private static String postgresql_driver;
private static String postgresql_url;
private static String postgresql_user;
private static String postgresql_password;
    
// 2、Greenplum
private static String greenplum_driver;
private static String greenplum_url;
private static String greenplum_user;
private static String greenplum_password;

 // 1、Postgresql
 static {
    postgresql_driver = ResourceBundle.getBundle("db").getString("postgresql_driver");
    postgresql_url = ResourceBundle.getBundle("db").getString("postgresql_url");
    postgresql_user = ResourceBundle.getBundle("db").getString("postgresql_user");
    postgresql_password = ResourceBundle.getBundle("db").getString("postgresql_password");
}
// 2、Greenplum
 static {
    greenplum_driver = ResourceBundle.getBundle("db").getString("greenplum_driver");
    greenplum_url = ResourceBundle.getBundle("db").getString("greenplum_url");
    greenplum_user = ResourceBundle.getBundle("db").getString("greenplum_user");
    greenplum_password = ResourceBundle.getBundle("db").getString("greenplum_password");
}    
// 1、Postgresql
 public static Connection getPostgresqlConnection() throws ClassNotFoundException, SQLException {
    // 加载数据库驱动
    Class.forName(postgresql_driver);
    // System.out.println("测试加载数据库成功");
    Connection con = DriverManager.getConnection(postgresql_url, postgresql_user, postgresql_password);
    // System.out.println("测试数据库链接成功");
    return con;
}
// 2、Greenplum
 public static Connection getGreenplumConnection() throws ClassNotFoundException, SQLException {
    // 加载数据库驱动
    Class.forName(greenplum_driver);
    //System.out.println("测试加载数据库成功");
    Connection con = DriverManager.getConnection(greenplum_url, greenplum_user, greenplum_password);
    //System.out.println("测试数据库链接成功");
    return con;
}
  public static void main(String[] args) {
        try {
            JdbcUtils.getPostgresqlConnection();
            System.out.println("汇聚数据区连接成功.....");
            System.out.println("=======================================");
            
            JdbcUtils.getGreenplumConnection();
            System.out.println("核心数据区连接成功.....");
            System.out.println("=======================================");
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

4、最后,可以根据公司业务进行一些增删改查操作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值