您的位置:首页 > 编程语言 > Java开发

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

2018-10-09 15:26 691 查看

1、公司使用的Greenplum和Postgresql,确实让我学到不少东西。简单将使用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、最后,可以根据公司业务进行一些增删改查操作。略。

待续......

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: