JDBC工具类_JdbcUtils
程序员文章站
2022-03-26 22:24:36
抽取JDBC工具类 : JDBCUtils目的:简化书写分析:注册驱动也抽取抽取一个方法获取连接对象需求:不想传递参数(麻烦),还得保证工具类的通用性。解决:配置文件jdbc.propertiesurl=user=password=抽取一个方法释放资源抽取注册驱动,抽取一个方法释放资源/** * JDBC工具类 */public class JDBCUtils { private static String url; privat...
抽取JDBC工具类 : JDBCUtils
-
目的:简化书写
-
分析:
-
注册驱动也抽取
-
抽取一个方法获取连接对象
需求:不想传递参数(麻烦),还得保证工具类的通用性。
解决:配置文件
jdbc.properties
url=
user=
password= -
抽取一个方法释放资源
-
-
分析:
抽取注册驱动,抽取一个方法释放资源
/**
* JDBC工具类
*/
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的读取,只需要读取一次即可拿到这些值。使用静态代码块
*/
static {
try {
//读取文件,获取值
//1.创建Properties集合类
Properties pro = new Properties();
//获取src路径下的文件的方式 --->ClassLoader 类加载器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
System.out.println(path);
//2.加载文件
// pro.load(new FileReader("D:\\Learn\\IdeaProjects\\itcast\\day04_jdbc\\src\\jdbc.properties"));
pro.load(new FileReader(path));
//3.获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//4.注册驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取连接
*
* @return 连接对象
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放资源
*
* @param stmt
* @param conn
*/
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在src下创建jdbc.properties配置文件
url=jdbc:mysql:///db3?serverTimezone=UTC
user=root
password=123
driver=com.mysql.cj.jdbc.Driver
演示JDBC工具类:
public class JDBCDemo06 {
public static void main(String[] args) {
List<Emp> list = new JDBCDemo06().findAll2();
for (Emp emp : list) {
System.out.println(emp);
}
}
/**
* 演示JDBC工具类
*/
public List<Emp> findAll2(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Emp> list = null;
try {
//1.注册驱动
/* Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取connection 对象
conn = DriverManager.getConnection("jdbc:mysql:///db3?serverTimezone=UTC","root","123");
*/
conn = JDBCUtils.getConnection();
//3.定义sql
String sql = "select * from emp";
//4.获取执行sql语句的statement
stmt = conn.createStatement();
//5.执行sql
rs = stmt.executeQuery(sql);
//6.遍历结果集,封装对象,装载对象
Emp emp = null;
list = new ArrayList<Emp>();
while(rs.next()){
//获取数据
int id = rs.getInt(1);
String ename = rs.getString(2);
int job_id = rs.getInt(3);
int mgr = rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double salary = rs.getDouble(6);
double bonus = rs.getDouble("bonus");
int dept_id = rs.getInt("dept_id");
//创建emp 对象,并赋值
emp = new Emp();
emp.setId(id);
emp.setEname(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setSalary(salary);
emp.setBonus(bonus);
emp.setDept_id(dept_id);
//装载集合
list.add(emp);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//7.释放资源
//注意避免空指针异常
/* if (rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}*/
JDBCUtils.close(rs,stmt,conn);
}
return list;
}
}
程序演示:
本文地址:https://blog.csdn.net/weixin_44664432/article/details/109573273