jason0401 发表于 2016-11-19 06:29:18

postgresql jdbc

package ;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil_Postgresql {
/**
* 取得数据库连接
* @return Connection
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://10.10.33.46:5432/postgis_test";
String user = "postgres";
String password = "000000";
conn = DriverManager.getConnection(url, user, password);
System.out.println("conn: "+conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
DBUtil_Postgresql.getConnection();
long end = System.currentTimeMillis();
System.out.println("time: "+(end - start));
}
}
页: [1]
查看完整版本: postgresql jdbc