日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DBUtil工具

發布時間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DBUtil工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DBUtil:
DBUtils 是一個jdbc的工具,使用的范圍內非常廣,主要是為了簡化jdbc的代碼
核心類:QueryRunner; ResultSetHandler
核心方法:

  • update();用來執行DDL(DDL:create alert,drop;);
  • query();用來執行DML(DML:insert update delete;);
  • batch(); 用來執行批處理;
  • 調用本方法之前,需要先創建對象,代碼如下:
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

DBUtil工具類的創建:

//創建一個接口以實現查詢功能 interface IRowMapper{void rowMapper(ResultSet res); }public class DBUtil {//因為加載驅動的代碼只需要執行一次即可,沒必要重復執行,所以封裝到靜態代碼塊中static {try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}}//將建立連接的代碼抽取出來,提高代碼的復用性private static Connection getConnection() {try {return DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "0314");} catch (SQLException e) {e.printStackTrace();}return null;}//實現查詢功能public static void select(String sql, IRowMapper rowMapper) {Connection connection = null;Statement statement = null;ResultSet result = null;try {connection = getConnection();statement = connection.createStatement();result = statement.executeQuery(sql);rowMapper.rowMapper(result);} catch (Exception e) {e.printStackTrace();}finally {close(result, statement, connection);}}//實現增刪改功能public static boolean update(String sql) {Connection connection = null;Statement statement = null;try {connection = getConnection();statement = connection.createStatement();return statement.executeUpdate(sql) > 0;} catch (Exception e) {e.printStackTrace();}finally {close(statement, connection);}return false;}//將關閉資源代碼抽取出來,提高代碼的復用性private static void close(Statement statement, Connection connection) {try {if (statement != null) {statement.close();}} catch (SQLException e) {e.printStackTrace();}try {if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}//close方法的重載private static void close(ResultSet result, Statement statement, Connection connection) {try {if (result != null) {result.close();}result = null;} catch (SQLException e) {e.printStackTrace();}close(statement, connection);} }

DBUtil工具類的使用:

public class Select {public static void main(String[] args){String sql = "select * from user_info";class RowMapper implements IRowMapper{@Overridepublic void rowMapper(ResultSet res) {try {while (res.next()) {String student_id = res.getString("id");String userName = res.getString("user_name");String password = res.getString("password");System.out.println(student_id+" ,"+userName+" ,"+password);}} catch (SQLException e) {e.printStackTrace();}} }IRowMapper rowMapper = new RowMapper();DBUtil.select(sql, rowMapper);} }

使用DBUtil工具類實現刪除功能:

public class Update {public static void main(String[] args) {String sql = "delete from user_info";if (DBUtil.update(sql)) {System.out.println("YSE");}else {System.out.println("NO");}} }

總結

以上是生活随笔為你收集整理的DBUtil工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。