上传者: fuhaifuyun
|
上传时间: 2022-03-08 16:48:51
|
文件大小: 1.54MB
|
文件类型: -
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();
}
}
}