public class JDBCUtils {
private static String drivername;
private static String url;
private static String user;
private static String password;
private static Properties pro = new Properties();
static{
try {
//加载,读取jdbc.properties配置的信息
//pro.load的作用是把jdbc.properties文件中配置的信息,一一put到pro这个map中
pro.load(ClassLoader.getSystemClassLoader().getResourceAsStream(“jdbc.properties”));
// drivername = pro.getProperty(“key”)
drivername = pro.getProperty(“drivername”);
url = pro.getProperty(“url”);
user = pro.getProperty(“user”);
password = pro.getProperty(“password”);
//注册驱动,加载驱动
Class.forName(drivername);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection()throws SQLException{
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public static void closeQuietly(Connection conn){
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeQuietly(Statement st){
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeQuietly(ResultSet rs){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeQuietly(Statement st,Connection conn){
closeQuietly(st);
closeQuietly(conn);
}
public static void closeQuietly(ResultSet rs,Statement st,Connection conn){
closeQuietly(rs);
closeQuietly(st);
closeQuietly(conn);
}
}
评论