Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import com.mchange.v2.c3p0.DataSources;
  2. import javax.sql.DataSource;
  3. import java.sql.*;
  4. import java.util.Properties;
  5. /**
  6. * Created by addo on 2017/4/10.
  7. */
  8. public class JDBCTest {
  9. private static String POSTGRESQL_URL = "jdbc:postgresql://192.168.56.101:5432/example";
  10. private static String POSTGRESQL_USERNAME = "dbuser";
  11. private static String POSTGRESQL_PASSWORD = "password";
  12. private static String GPDB_URL = "jdbc:pivotal:greenplum://192.168.56.101:5432;DatabaseName=test";
  13. private static String GPDB_USERNAME = "dbuser";
  14. private static String GPDB_PASSWORD = "password";
  15. /**
  16. * Postgresql Connection
  17. *
  18. * @return
  19. * @throws ClassNotFoundException
  20. * @throws SQLException
  21. */
  22. public static Connection postgresqlConnection() throws ClassNotFoundException, SQLException {
  23. Class.forName("org.postgresql.Driver");
  24. return DriverManager.getConnection(POSTGRESQL_URL, POSTGRESQL_USERNAME, POSTGRESQL_PASSWORD);
  25. }
  26. /**
  27. * GreenPlum Connection
  28. *
  29. * @return
  30. * @throws ClassNotFoundException
  31. * @throws SQLException
  32. */
  33. public static Connection gpdbConnection() throws ClassNotFoundException, SQLException {
  34. Class.forName("com.pivotal.jdbc.GreenplumDriver");
  35. return DriverManager.getConnection(GPDB_URL, GPDB_USERNAME, GPDB_PASSWORD);
  36. }
  37. /**
  38. * GreenPlud C3P0 Datasource Connection
  39. *
  40. * @return
  41. * @throws SQLException
  42. */
  43. public static Connection gpdbC3P0Connection() throws SQLException {
  44. Properties c3p0Props = new Properties();
  45. c3p0Props.setProperty("driverClass", "com.pivotal.jdbc.GreenplumDriver");
  46. c3p0Props.setProperty("jdbcUrl", GPDB_URL);
  47. c3p0Props.setProperty("user", GPDB_USERNAME);
  48. c3p0Props.setProperty("password", GPDB_PASSWORD);
  49. c3p0Props.setProperty("acquireIncrement", "5");
  50. c3p0Props.setProperty("initialPoolSize1", "1");
  51. c3p0Props.setProperty("maxIdleTime", "60");
  52. c3p0Props.setProperty("maxPoolSize", "50");
  53. c3p0Props.setProperty("minPoolSize", "1");
  54. c3p0Props.setProperty("idleConnectionTestPeriod", "60");
  55. return DataSources.unpooledDataSource(GPDB_URL, c3p0Props).getConnection();
  56. }
  57. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  58. Connection[] connections = new Connection[]{postgresqlConnection(), gpdbConnection(), gpdbC3P0Connection()};
  59. for (Connection connection : connections) {
  60. CallableStatement callableStatement = connection.prepareCall("select * from user");
  61. boolean execute = callableStatement.execute();
  62. ResultSet resultSet = callableStatement.getResultSet();
  63. while (resultSet.next()) {
  64. System.out.println(resultSet.getString("current_user"));
  65. }
  66. callableStatement.close();
  67. connection.close();
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement