GreenPlum JDBC和C3P0数据源

在网上搜索GreenPlum(GPDB)的数据源配置的时候,发现搜索结果都是用postgresql的配置。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import com.mchange.v2.c3p0.DataSources;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

/**
 * Created by addo on 2017/4/10.
 */
public class JDBCTest {

    private static String POSTGRESQL_URL = "jdbc:postgresql://192.168.56.101:5432/example";
    private static String POSTGRESQL_USERNAME = "dbuser";
    private static String POSTGRESQL_PASSWORD = "password";

    private static String GPDB_URL = "jdbc:pivotal:greenplum://192.168.56.101:5432;DatabaseName=test";
    private static String GPDB_USERNAME = "dbuser";
    private static String GPDB_PASSWORD = "password";

    /**
     * Postgresql Connection
     *
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection postgresqlConnection() throws ClassNotFoundException, SQLException {
        Class.forName("org.postgresql.Driver");
        return DriverManager.getConnection(POSTGRESQL_URL, POSTGRESQL_USERNAME, POSTGRESQL_PASSWORD);
    }

    /**
     * GreenPlum Connection
     *
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection gpdbConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.pivotal.jdbc.GreenplumDriver");
        return DriverManager.getConnection(GPDB_URL, GPDB_USERNAME, GPDB_PASSWORD);
    }

    /**
     * GreenPlud C3P0 Datasource Connection
     *
     * @return
     * @throws SQLException
     */
    public static Connection gpdbC3P0Connection() throws SQLException {
        Properties c3p0Props = new Properties();
        c3p0Props.setProperty("driverClass", "com.pivotal.jdbc.GreenplumDriver");
        c3p0Props.setProperty("jdbcUrl", GPDB_URL);
        c3p0Props.setProperty("user", GPDB_USERNAME);
        c3p0Props.setProperty("password", GPDB_PASSWORD);
        c3p0Props.setProperty("acquireIncrement", "5");
        c3p0Props.setProperty("initialPoolSize1", "1");
        c3p0Props.setProperty("maxIdleTime", "60");
        c3p0Props.setProperty("maxPoolSize", "50");
        c3p0Props.setProperty("minPoolSize", "1");
        c3p0Props.setProperty("idleConnectionTestPeriod", "60");
        return DataSources.unpooledDataSource(GPDB_URL, c3p0Props).getConnection();
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection[] connections = new Connection[]{postgresqlConnection(), gpdbConnection(), gpdbC3P0Connection()};
        for (Connection connection : connections) {
            CallableStatement callableStatement = connection.prepareCall("select * from user");
            boolean execute = callableStatement.execute();
            ResultSet resultSet = callableStatement.getResultSet();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("current_user"));
            }
            callableStatement.close();
            connection.close();
        }

    }
}

源代码

Comments

comments powered by Disqus