jdbcutil封装成一个工具类

package review.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {

public static Connection getConnection() {
Connection conn=null;
InputStream is=JDBCUtils.class.getClassLoader().getResourceAsStream(“jdbc.properties”);
Properties pros=new Properties();
try {
pros.load(is);
String driver=pros.getProperty(“driver”);
String url=pros.getProperty(“url”);
String user=pros.getProperty(“user”);
String password=pros.getProperty(“password”);
Class.forName(driver);
conn=DriverManager.getConnection(
url,
user,
password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeRes(Connection conn,PreparedStatement ps, ResultSet rs) {
try {
if(conn!=null) {
conn.close();
}if(ps!=null) {
ps.close();
}if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

上一篇:CentOS随笔 - 6.CentOS7安装Git服务器


下一篇:chunked lod pros and cons