一、概念
目的是爲了簡化書寫
分析:
(1)註冊驅動也抽取
(2)抽取一個方法獲取連接對象
* 需求:不想傳遞參數(麻煩),還得保證工具類的通用性。
* 解決:配置文件
jdbc.properties
url=
user=
password=
(3)抽取一個方法釋放資源
二、代碼實現
- 編寫配置文件:jdbc.properties(需要將其放在同項目的 src 目錄下)
url=jdbc:mysql:///db
user=root
password=root
driver=com.mysql.jdbc.Driver
這樣以後每次更換數據庫,只需要修改此配置文件即可,通過調用進行使用
2. 編寫 JDBCUtils 實現對 上述分析的需求進行封裝
/**
* 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路徑
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();//將其轉換爲字符串
//2. 加載文件
pro.load(new FileReader(path));
//3. 獲取數據,賦值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//4. 註冊驅動
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException 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();
}
}
}
/**
* 釋放資源
* @param stmt
* @param conn
*/
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();
}
}
}
}
調用 JDBC 工具類的 jdbcDemo
/**
* * 定義一個方法,查詢emp表的數據將其封裝爲對象,然後裝載集合,返回。
*/
public class JDBCDemo8 {
public static void main(String[] args) {
List<Emp> list = new JDBCDemo8().findAll();
System.out.println(list);
System.out.println(list.size());
}
/**
* 演示JDBC工具類
* @return
*/
public List<Emp> findAll(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Emp> list = null;
try {
conn = JDBCUtils.getConnection();
//3.定義sql
String sql = "select * from emp";
//4.獲取執行sql的對象
stmt = conn.createStatement();
//5.執行sql
rs = stmt.executeQuery(sql);
//6.遍歷結果集,封裝對象,裝載集合
Emp emp = null;
list = new ArrayList<Emp>();
while(rs.next()){
//獲取數據
int id = rs.getInt("id");
String ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr = rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double salary = rs.getDouble("salary");
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 e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,stmt,conn);
}
return list;
}
}