jdbc封装工具类代码_JDBC的使用-JDBC(3)
光有理論,沒有實踐是不行的,本篇文章就是介紹JDBC如何使用。
本文知識點分布如下:
- 下載數據庫驅動
- 創建項目添加驅動
- 通過 Statement 向表中插入數據
- 通過 Statement 對象修改表中的數據
- 封裝 JDBC 工具類
- 通過 Statement
- ResultSet 講解
- 通過 ResultSet 實現邏輯分頁
- 什么是 SQL 注入?
- PreparedStatement 對象的使用(重點)
- PreparedStatement 的預編譯能力
- 通過 PreparedStatement
- 通過 PreparedStatement 對象完成數據的查詢
- PreparedStatement
- JDBC 中的事務
三、 JDBC 的使用
加載數據庫驅動程序 → 建立數據庫連接 Connection → 創建執行 SQL 的語句
Statement → 處理執行結果 ResultSet → 釋放資源。
1 下載數據庫驅動
1.1 MySQL 驅動
Download Connector/J?dev.mysql.com1.2 Oracle 驅動
數據庫安裝目錄oracleproduct11.2.0dbhome_1jdbclib
2 創建項目添加驅動
3 通過 Statement 向表中插入數據
3.1 注冊驅動
Class.forName("com.mysql.jdbc.Driver");3.2 獲取鏈接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root", "root");3.3 執行 SQL
String sql="insert into departments values(default,'"+department_name+"'"+location_id+")"; Statement state = conn.createStatement();3.4 釋放資源
if(state != null){ try { state.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }4 通過 Statement 對象修改表中的數據
4.1 代碼:
//更新 departments 表中的 department_id 為 6 的數據,將部門名稱修改為教學部,location_id 修改為 6。5 封裝 JDBC 工具類
5.1 普通版
5.1.1 工具類代碼.
/** * jdbc 工具類 * @author Alan * */ public class JdbcUtil { private static String driver = "com.mysql.jdbc.Driver"; private static String jdbcUrl="jdbc:mysql://localhost:3306/bjsxt?useUnicode =true&characterEncoding=utf-8"; private static String username ="root"; private static String userpassword="root"; static{ try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取 Connection 對象 public static Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(jdbcUrl,username, userpassword); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //關閉 Statement public static void closeStatement(Statementstate){ try { if(state != null){ state.close(); } } catch (SQLException e) { e.printStackTrace(); } } //關閉 Connection public static void closeConnection(Connection conn){ try { if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } //關閉資源 public static void closeResource(Statement state,Connection conn){ closeStatement(state); closeConnection(conn); } }5.2 升級版
5.2.1 工模具類代碼
/** * jdbc 工具類 * @author Administrator * */ public class JdbcUtil { private static String driver; private static String jdbcUrl; private static String username; private static String userpassword; static{ //讀取 Properties 文件 ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); driver = bundle.getString("driver"); jdbcUrl= bundle.getString("jdbcUrl"); username = bundle.getString("username"); userpassword =bundle.getString("userpassword"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取 Connection 對象 public static Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(jdbcUrl, username, userpassword); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //關閉 Statement public static void closeStatement(Statement state){ try { if(state != null){ state.close(); } } catch (SQLException e) { e.printStackTrace(); } } //關閉 Connection public static void closeConnection(Connection conn){ try { if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } //關閉資源 public static void closeResource(Statement state,Connection conn){ closeStatement(state); closeConnection(conn); } }6 通過 Statement 對象查詢數據
6.1 代碼
//查詢 Departmetns 表中部門 ID 為 6 的部門信息 public void selectDepartmentsById(int departmentId){ Connection conn = null; Statement state = null; ResultSet rs = null; try{ conn= JdbcUtil.getConnection(); state = conn.createStatement(); String sql = "select * from departments d where d.department_id = "+departmentId; //執行查詢返回結果 rs=state.executeQuery(sql); while(rs.next()){ System.out.println(rs.getInt("department_id")+" "+rs.getString("department_name")+" "+rs.getInt(3)); } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(state, conn,rs); } }7 ResultSet 講解
注意 ResultSet 中封裝的并不是我們查詢到的所有的結果集,而是返回了查詢到的結果
集的數據庫游標。通過 ResultSet 中的 next()方法操作游標的位置獲取結果集。
8 通過 ResultSet 實現邏輯分頁
8.1 代碼
//查詢 departments 表中的所有的數據,并且通過 ResultSet 實現邏輯分 頁 public void selectDeptPage(int currentPage,int pageRows){ Connection conn = null; Statement state = null; ResultSet rs = null; try{ conn = JdbcUtil.getConnection(); state = conn.createStatement(); String sql = "select * from departments"; rs = state.executeQuery(sql); //開始位置與結束位置 int begin = (currentPage -1)*pageRows; int end = currentPage * pageRows; //當前位置的計數器 int currentNum = 0; while(rs.next()){ //什么情況下獲取結果集中的數據 if(currentNum >= begin && currentNum < end){ System.out.println(rs.getInt("department_id")+" "+rs.getString("department_name")); //結束操作 ResultSet 的邊界條件 if(currentNum == end -1){ break; } } currentNum++; } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(state, conn, rs); } }9 SQL 注入問題
9.1 什么是 SQL 注入
所謂 SQL 注入,就是通過把含有 SQL 語句片段的參數插入到需要執行的 SQL 語句中,最終達到欺騙數據庫服務器執行惡意操作的 SQL 命令。
9.2 SQL 注入案例
//sql 注入 public void sqlInject(String departmentName,int locationId){ Connection conn = null; Statement state = null; ResultSet rs = null; try{ conn = JdbcUtil.getConnection(); state = conn.createStatement(); String sql = "select * from departments where department_name ='"+departmentName+"' and location_id = "+locationId; System.out.println(sql); rs = state.executeQuery(sql); while(rs.next()){ System.out.println(rs.getInt("department_id")+" "+rs.getString("department_name")+" "+rs.getInt("location_id")); } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(state, conn, rs); } }10 PreparedStatement 對象的使用( 重點)
10.1 PreparedStatement 特點:
? PreparedStatement 接口繼承 Statement 接口
? PreparedStatement 效率高于 Statement
? PreparedStatement 支持動態綁定參數
? PreparedStatement 具備 SQL 語句預編譯能力
? 使用 PreparedStatement 可防止出現 SQL 注入問題
10.2 通過 PreparedStatement 對象向表中插入數據
10.2.1
//向 Departments 表中插入一條數據 public void insertDempartments(String departmentName,int locationId){ Connection conn = null; PreparedStatement ps = null; try{ conn = JdbcUtil.getConnection(); ps = conn.prepareStatement("insert into departments values(default,?,?)"); ps.setString(1, departmentName); ps.setInt(2, locationId); ps.execute(); }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(ps, conn, null); } }11 PreparedStatement 的預編譯能力
11.1 什么是預編譯
11.1.1 SQL
? 語法和語義解析
? 優化 sql 語句,制定執行計劃
? 執行并返回結果
但是很多情況,我們的一條 sql 語句可能會反復執行,或者每次執行的時候只有個別的
值不同(比如 select 的 where 子句值不同,update 的 set 子句值不同,insert 的 values 值不同)。
如果每次都需要經過上面的詞法語義解析、語句優化、制定執行計劃等,則效率就明顯不行
了。
所謂預編譯語句就是將這類語句中的值用占位符替代,可以視為將 sql 語句模板化或者
說參數化
預編譯語句的優勢在于:一次編譯、多次運行,省去了解析優化等過程;此外預編譯語
句能防止 sql 注入。
11.1.2 解析過程
11.1.2.1 硬解析
在不開啟緩存執行計劃的情況下,每次 SQL 的處理都要經過:語法和語義的解析,優
化器處理 SQL,生成執行計劃。整個過程我們稱之為硬解析。
11.1.2.2 軟解析
如果開啟了緩存執行計劃,數據庫在處理 sql 時會先查詢緩存中是否含有與當前 SQL
語句相同的執行計劃,如果有則直接執行該計劃。
11.2 預編譯方式
開始數據庫的日志
show VARIABLES like '%general_log%'
set GLOBAL general_log = on
set GLOBAL log_output='table'
11.2.1 依賴數據庫驅動完成預編譯
如果我們沒有開啟數據庫服務端編譯,那么默認的是使用數據庫驅動完成 SQL 的預編譯處理。
11.2.2 依賴數據庫服務器完成預編譯
我們可以通過修改連接數據庫的 URL 信息,添加 useServerPrepStmts=true 信息開啟服務端預編譯。
12 通過 PreparedStatement 對象完成數據的更新
12.1 代碼示例:
//更新數據 public void updateDepartment(int departmentId,String departmentName,int localhostId){ Connection conn= null; PreparedStatement ps = null; try{ conn = JdbcUtil.getConnection(); ps = conn.prepareStatement("update departments set department_name = ?,location_id = ? where department_id = ?"); ps.setString(1, departmentName); ps.setInt(2, localhostId); ps.setInt(3, departmentId); ps.execute(); }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(ps, conn, null); } }13 通過 PreparedStatement 對象完成數據的查詢
13.1 查詢返回單條結果集
13.1.1 代碼示例:
//完成數據查詢 public Departments selectDepartmentsById(int departmentId){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Departments dept = null; try{ conn = JdbcUtil.getConnection(); ps = conn.prepareStatement("select * from departments where department_id = ?"); ps.setInt(1, departmentId); rs = ps.executeQuery(); while(rs.next()){ dept=new Departments(); dept.setDepartmentId(rs.getInt("department_id")); dept.setDepartmentName(rs.getString("department_name")); dept.setLocationId(rs.getInt("location_id")); } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(ps, conn, rs); } return dept; }13.2 查詢返回多條結果集
13.2.1 代碼示例:
//查詢部門表中的部門名稱,找到那些包含“人力”的部門信息 public List<Departments> selectDepartmentByLikeName(String departmentName){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List<Departments> list = new ArrayList<>(); try{ conn = JdbcUtil.getConnection(); ps = conn.prepareStatement("select * from departments where department_name like ?"); ps.setString(1, "%"+departmentName+"%"); rs = ps.executeQuery(); while(rs.next()){ Departments dept = new Departments(); dept.setDepartmentId(rs.getInt("department_id")); dept.setDepartmentName(rs.getString("department_name")); dept.setLocationId(rs.getInt("location_id")); list.add(dept); } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(ps, conn, rs); } return list; }14 PreparedStatement 批處理操作
批處理:在與數據庫的一次連接中,批量的執行條 SQL 語句。
14.1 代碼示例:
//批量添加 public void addBatch(List<Departments> list){ Connection conn = null; PreparedStatement ps = null; try{ conn = JdbcUtil.getConnection(); ps = conn.prepareStatement("insert into departments values(default,?,?)"); for(int i=0;i<list.size();i++){ ps.setString(1, list.get(i).getDepartmentName()); ps.setInt(2, list.get(i).getLocationId()); //添加批處理 ps.addBatch(); } int[] arr =ps.executeBatch(); for(int i=0;i<arr.length;i++){ System.out.println(i); } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtil.closeResource(ps, conn, null); } }15 JDBC 中的事務處理
在 JDBC 操作中數據庫事務默認為自動提交。如果事務需要修改為手動提交,那么我們
需要使用 Connection 對象中的 setAutoCommit 方法來關閉事務自動提交。然后通過
Connection 對象中的 commit 方法與 rollback 方法進行事務的提交與回滾。
15.1 代碼
//事務處理 public void deleteDempartments(String depratmentName){ Connection conn = null; PreparedStatement ps = null; try{ conn = JdbcUtil.getConnection(); //關閉事務的自動提交 conn.setAutoCommit(false); ps = conn.prepareStatement("delete from departments where department_name like ?"); ps.setString(1, "%"+depratmentName+"%"); ps.executeUpdate(); ps = conn.prepareStatement("insert into departments values(default,'開發部',2)"); ps.executeUpdate(); String str = null; str.length(); conn.commit(); }catch(Exception e){ e.printStackTrace(); JdbcUtil.rollback(conn); }finally{ JdbcUtil.closeResource(ps, conn, null); } }感謝~,歡迎點贊轉發,以及指正。
總結
以上是生活随笔為你收集整理的jdbc封装工具类代码_JDBC的使用-JDBC(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 声波武器在中国诞生,它的威力有多大?
- 下一篇: python开发板比较_再不学点Pyth