1. 程式人生 > >java 使用jdbc連線Greenplum資料庫和Postgresql資料庫

java 使用jdbc連線Greenplum資料庫和Postgresql資料庫

 1 public class JdbcUtils {
 2 
 3     // 1、Postgresql
 4     private static String postgresql_driver;
 5     private static String postgresql_url;
 6     private static String postgresql_user;
 7     private static String postgresql_password;
 8         
 9     // 2、Greenplum
10     private static
String greenplum_driver; 11 private static String greenplum_url; 12 private static String greenplum_user; 13 private static String greenplum_password; 14 15 // 1、Postgresql 16 static { 17 postgresql_driver = ResourceBundle.getBundle("db").getString("postgresql_driver
"); 18 postgresql_url = ResourceBundle.getBundle("db").getString("postgresql_url"); 19 postgresql_user = ResourceBundle.getBundle("db").getString("postgresql_user"); 20 postgresql_password = ResourceBundle.getBundle("db").getString("postgresql_password"); 21 } 22 23
// 2、Greenplum 24 static { 25 greenplum_driver = ResourceBundle.getBundle("db").getString("greenplum_driver"); 26 greenplum_url = ResourceBundle.getBundle("db").getString("greenplum_url"); 27 greenplum_user = ResourceBundle.getBundle("db").getString("greenplum_user"); 28 greenplum_password = ResourceBundle.getBundle("db").getString("greenplum_password"); 29 } 30 31 32 // 1、Postgresql 33 public static Connection getPostgresqlConnection() throws ClassNotFoundException, SQLException { 34 // 載入資料庫驅動 35 Class.forName(postgresql_driver); 36 // System.out.println("測試載入資料庫成功"); 37 Connection con = DriverManager.getConnection(postgresql_url, postgresql_user, postgresql_password); 38 // System.out.println("測試資料庫連結成功"); 39 return con; 40 } 41 42 // 2、Greenplum 43 public static Connection getGreenplumConnection() throws ClassNotFoundException, SQLException { 44 // 載入資料庫驅動 45 Class.forName(greenplum_driver); 46 //System.out.println("測試載入資料庫成功"); 47 Connection con = DriverManager.getConnection(greenplum_url, greenplum_user, greenplum_password); 48 //System.out.println("測試資料庫連結成功"); 49 return con; 50 } 51 52 public static void main(String[] args) { 53 try { 54 JdbcUtils.getPostgresqlConnection(); 55 System.out.println("匯聚資料區連線成功....."); 56 System.out.println("======================================="); 57 58 JdbcUtils.getGreenplumConnection(); 59 System.out.println("核心資料區連線成功....."); 60 System.out.println("======================================="); 61 62 } catch (ClassNotFoundException e) { 63 e.printStackTrace(); 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } 67 } 68 }