Java-JDBC-JDBCUtils工具類

Java-JDBC-JDBCUtils工具類


目錄

文章目錄




內容

  之前關於JDBC的測試,都有一些相同的步驟:

  1. 數據庫連接
  2. 資源釋放

每次都要書寫相同的步驟,是不是很麻煩?那麼我們就可以編寫個工具類,把這些共同的操作放一起,方便以後使用。

  • 工具類代碼分析:

    • 需要一個獲取連接對象的方法getConnection
      • getConnection所需參數url, username, password通過配置文件獲取
      • 只需要讀取一次,可以在靜態代碼塊中配置
    • 需要釋放資源的方法close
  • 工具類代碼:

      package cn.gaogzhen.util;
    
      import java.io.IOException;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      import java.util.Properties;
    
      public class JDBCUtils {
      	private static Connection conn;
      	/**
      	 * 靜態代碼塊,獲取配置信息
      	 */
      	static {
    
      		try {
      			Properties prop = new Properties();
      			prop.load(JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
      			String url = prop.getProperty("url");
      			String username = prop.getProperty("username");
      			String password = prop.getProperty("password");
      			String driver = prop.getProperty("driver");
      			// 註冊驅動
      			Class.forName(driver);
      			conn = DriverManager.getConnection(url, username, password);
    
      		} catch (IOException | ClassNotFoundException | SQLException e) {
      			e.printStackTrace();
      		} 
      	}
    
      	public static Connection getConnection() throws SQLException {
      		return conn;
      	}
    
      	/**
      	 * 釋放資源
      	 * @param rs 	結果集對象
      	 * @param stmt	執行SQL語句對象
      	 * @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();
      			}
      		}
      	}
    
      	public static void close(Statement stmt, Connection conn) {
      		close(null, stmt, conn);
      	}
      }
    
  • 使用測試:獲取表emp中所有數據,封裝對象並放入集合中-改造

      // 之前沒用工具類代碼在JDBC快速入門博文中
      // 改造後
      package cn.gaogzhen.jdbc;
    
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
    
      import cn.gaogzhen.domain.Emp;
      import cn.gaogzhen.util.JDBCUtils;
    
      public class JDBCDemo6 {
      	public static void main(String[] args) {
      		List<Emp> list = new JDBCDemo6().findAll();
      		System.out.println(list);
      	 }
    
      	public List<Emp> findAll() {
      		Connection conn = null;
      		Statement stmt = null;
      		ResultSet rs = null;
      		List<Emp> list = new ArrayList<>();
      		 // 2. 註冊驅動
      		try {
    
      		   conn = JDBCUtils.getConnection();
      			// 4. sql語句
      			String sql = "select * from emp";
      			// 5. 獲取執行sql語句的對象statement
      			stmt = conn.createStatement();
      			// 6. 執行sql操作,獲取結果
      			rs = stmt.executeQuery(sql);
      			// 7. 解析結果
    
      			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");
      			   list.add(new Emp(id, ename, job_id, mgr, joindate, salary, bonus, dept_id));
      		   }
      		} catch (SQLException e) {
      			e.printStackTrace();
      		} finally {
      			 // 8. 釋放資源
      			JDBCUtils.close(rs, stmt, conn);
      		}
      		return list;
      	}
      }
    

測試結果:

[Emp [id=1001, name=孫悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=8000.0, bonus=0.0, dept_id=20], Emp [id=1002, name=盧俊義, job_id=3, mgr=1006, joindate=2001-02-20, salary=16000.0, bonus=3000.0, dept_id=30], Emp [id=1003, name=林沖, job_id=3, mgr=1006, joindate=2001-02-22, salary=12500.0, bonus=5000.0, dept_id=30], Emp [id=1004, name=唐僧, job_id=2, mgr=1009, joindate=2001-04-02, salary=29750.0, bonus=0.0, dept_id=20], Emp [id=1005, name=李逵, job_id=4, mgr=1006, joindate=2001-09-28, salary=12500.0, bonus=14000.0, dept_id=30], Emp [id=1006, name=宋江, job_id=2, mgr=1009, joindate=2001-05-01, salary=28500.0, bonus=0.0, dept_id=30], Emp [id=1007, name=劉備, job_id=2, mgr=1009, joindate=2001-09-01, salary=24500.0, bonus=0.0, dept_id=10], Emp [id=1008, name=豬八戒, job_id=4, mgr=1004, joindate=2007-04-19, salary=30000.0, bonus=0.0, dept_id=20], Emp [id=1009, name=羅貫中, job_id=1, mgr=0, joindate=2001-11-17, salary=50000.0, bonus=0.0, dept_id=10], Emp [id=1010, name=吳用, job_id=3, mgr=1006, joindate=2001-09-08, salary=15000.0, bonus=0.0, dept_id=30], Emp [id=1011, name=沙僧, job_id=4, mgr=1004, joindate=2007-05-23, salary=11000.0, bonus=0.0, dept_id=20], Emp [id=1012, name=李逵, job_id=4, mgr=1006, joindate=2001-12-03, salary=9500.0, bonus=0.0, dept_id=30], Emp [id=1013, name=小白龍, job_id=4, mgr=1004, joindate=2001-12-03, salary=30000.0, bonus=0.0, dept_id=20], Emp [id=1014, name=關羽, job_id=4, mgr=1007, joindate=2002-01-23, salary=13000.0, bonus=0.0, dept_id=10]]

後記

本項目爲參考某馬視頻開發,相關視頻及配套資料可自行度娘或者聯繫本人。上面爲自己編寫的開發文檔,持續更新。歡迎交流,本人QQ:806797785

前端項目源代碼地址:https://gitee.com/gaogzhen/vue-leyou
    後端JAVA源代碼地址:https://gitee.com/gaogzhen/JAVA

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章