1.数据库连接工具类
/**
* Author: hezishan
* Date: 2018/5/15.
* Description:
**/
public class DBUtils {
//资源文件
static Properties pros = null;
//静态初始化 当加载JDBCUtil类时调用
static {
pros = new Properties();
try {
//加载资源文件
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
if(in == null) {
throw new FileNotFoundException("配置文件未找到");
}
pros.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接
* @return Connection
*/
public static Connection getMySQLConnection() {
Connection conn = null;
try {
//加载数据库驱动
Class.forName(pros.getProperty("mysql.driver"));
//获取数据库连接
conn = DriverManager.getConnection(pros.getProperty("mysql.url"),
pros.getProperty("mysql.user"),pros.getProperty("mysql.pwd"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭JDBC资源 注意顺序
public static void close(ResultSet rs, Statement ps, Connection conn) {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps!=null){