论坛元老
- 威望
- 0
- 贡献
- 49
- 热心值
- 1
- 金币
- 5246
- 注册时间
- 2020-8-31
|
package com; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestJDBC { private Connection con ; private String user = "gpadmin" ; private String password = "123456" ; private String className = "com.pivotal.jdbc.GreenplumDriver " ; private String url = "jdbc:pivotal:greenplum://172.20.4.161:5432;DatabaseName=testDB" ; public static void main(String[] args) { // TODO Auto-generated method stub TestJDBC c = new TestJDBC(); c.Connect(); c.getCon(); c.closed(); } public void Connect() { try { Class. forName(className); System. out.println("加载数据库驱动成功!" ); } catch (ClassNotFoundException e ) { System. out.println("加载数据库驱动失败!" ); e.printStackTrace(); } } /** 创建数据库连接 */ public Connection getCon() { try { con = DriverManager. getConnection(url, user, password); System. out.println("创建数据库连接成功!" ); } catch (SQLException e ) { System. out.print(con ); System. out.println("创建数据库连接失败!" ); con = null; e.printStackTrace(); } return con ; } public void closed() { try { if (con != null) { con.close(); } } catch (SQLException e ) { System. out.println("关闭con对象失败!" ); e.printStackTrace(); } } }
|
|