Jdbc工具类(连接及释放)
package cn.gdpe.jdbc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtil {
public static String url=null;
public static String user=null;
public static String password=null;
public static String driverName=null;
public static void close(Connection conn,Statement st){
try {
if(conn!=null){
conn.close();
}
if(st!=null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn,Statement st ,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close(conn,st);
}
}
public static Connection getConnection(){
Connection conn=null;
try {
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
static {
try {
InputStream inputStream = JdbcUtil.class.getResourceAsStream("/db.properties");
Properties p=new Properties();
p.load(inputStream);
url=p.getProperty("url");
user=p.getProperty("user");
password=p.getProperty("password");
driverName=p.getProperty("driver");
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}