Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement