JDBC连接数据库工具类
import java.net.URL;
import java.sql.*;
public final class JdbcUtil {
private static String url ="jdbc:postgresql://localhost:5432/test";
private static String userName ="postgres";
private static String passWord ="postgres";
private JdbcUtil( ){
}
static {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url,userName,passWord);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn) {
try {
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
public static void close(Statement stmt){
try{
if(stmt != null){
stmt.close();
stmt = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
public static void close(ResultSet rs){
try{
if(rs != null){
rs.close();
rs = null;
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}