GreenPlum JDBC和C3P0数据源

GreenPlum JDBC和C3P0数据源

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

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 powered by Disqus