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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

【JavaWeb】JDBC优化 之 数据库连接池、Spring JDBC

發(fā)布時(shí)間:2024/7/5 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【JavaWeb】JDBC优化 之 数据库连接池、Spring JDBC 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 數(shù)據(jù)庫(kù)連接池

為什么要使用數(shù)據(jù)庫(kù)連接池?

  • 數(shù)據(jù)庫(kù)連接是一件費(fèi)時(shí)的操作,連接池可以使多個(gè)操作共享一個(gè)連接
  • 使用連接池可以提高對(duì)數(shù)據(jù)庫(kù)連接資源的管理
  • 節(jié)約資源且高效
  • 概念:數(shù)據(jù)庫(kù)連接池其實(shí)就是一個(gè)容器,存放數(shù)據(jù)庫(kù)連接的容器。當(dāng)系統(tǒng)初始化好后,容器被創(chuàng)建,容器中會(huì)申請(qǐng)一些連接對(duì)象,當(dāng)用戶訪問(wèn)數(shù)據(jù)庫(kù)時(shí),從容器中獲取連接對(duì)象,用戶訪問(wèn)完后,將連接對(duì)象歸還給數(shù)據(jù)庫(kù)。

    實(shí)現(xiàn):

  • 標(biāo)準(zhǔn)接口:DataSource:javax.sql包下。使用getConnection()獲取連接,如果連接對(duì)象是從連接池中獲取的。調(diào)用Connection.close方法則不會(huì)關(guān)閉連接,而是歸還連接。
  • 一般不去實(shí)現(xiàn)它,由數(shù)據(jù)庫(kù)廠商來(lái)實(shí)現(xiàn)。實(shí)現(xiàn)技術(shù)由:C3P0和Druid【阿里巴巴提供 最好的數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)技術(shù)】
  • 2 C3P0的使用

    步驟

  • 導(dǎo)入兩個(gè)c3p0的jar包
    mchange-commons-java-0.2.12
    c3p0-0.9.5.2
    還需要數(shù)據(jù)庫(kù)驅(qū)動(dòng)jar包
  • 定義配置文件
    名稱:c3p0.properties或者c3p0-config.xml
    路徑:直接將文件放在src目錄下即可
  • 創(chuàng)建核心對(duì)象,數(shù)據(jù)庫(kù)連接池對(duì)象 ComboPooledDataSource
  • 獲取連接:getConnection
  • public static void main(String[] args) throws SQLException {DataSource ds = new ComboPooledDataSource();ds.getConnection();System.out.println(ds);}

    3 Druid的使用

    步驟

  • 導(dǎo)入druid-1.0.9.jar包
  • 定義配置文件:druid.properties,可以叫任意名稱,可以放在任意目錄
  • 獲取數(shù)據(jù)庫(kù)連接池對(duì)象,通過(guò)工廠類DruidDataSourceFactory獲取
  • 獲取連接:getConnection
  • public class JDBCUtils {//1.定義成員變量 DataSourceprivate static DataSource ds ;static{try {//1.加載配置文件Properties pro = new Properties();pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//2.獲取DataSourceds = DruidDataSourceFactory.createDataSource(pro);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}/*** 獲取連接*/public static Connection getConnection() throws SQLException {return ds.getConnection();}/*** 釋放資源*/public static void close(Statement stmt,Connection conn){close(null,stmt,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 DataSource getDataSource(){return ds;} }

    4 Spring JDBC

    Spring框架對(duì)JDBC的簡(jiǎn)單封裝
    提供了一個(gè)JDBCTemplate對(duì)象來(lái)簡(jiǎn)化JDBC的開(kāi)發(fā)

    步驟:

  • 導(dǎo)入jar包
  • 創(chuàng)建JdbcTemplate對(duì)象,依賴于數(shù)據(jù)源DataSource
  • 使用JdbcTemplate對(duì)象的方法來(lái)完成CRUD的操作
    • update():執(zhí)行DML語(yǔ)句增刪改
    • qureyForMap:查詢結(jié)果并將結(jié)果集封裝為map集合
      注意:這個(gè)方法查詢的結(jié)果集長(zhǎng)度只能是1
    • qureyForList:查詢結(jié)果并將結(jié)果集封裝為list集合
      注意:將每一條記錄封裝為Map集合,再將Map集合裝載到List集合中
    • qurey():查詢結(jié)果,將結(jié)果封裝為JavaBean對(duì)象
      注意:qurey的參數(shù):RowMapper。一般使用BeanPropertyRowMapper實(shí)現(xiàn)類,可以完成數(shù)據(jù)到JavaBean的自動(dòng)封裝。
      -new BeanPropertyRowMapper<類型>(類型.class)
    • qureyForObject:查詢結(jié)果,將結(jié)果封裝為對(duì)象
      注意:一般用于聚合函數(shù)的查詢

    4.1 JDBC Template入門使用

  • 已經(jīng)導(dǎo)入的數(shù)據(jù)庫(kù)連接jar包
  • JDBCUtils是用druid實(shí)現(xiàn)的工具類,druid包也需要導(dǎo)入
  • 導(dǎo)入JDBC Template的五個(gè)jar包
  • import org.springframework.jdbc.core.JdbcTemplate;public class JDBCTemplateDemo {public static void main(String[] args) {JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());String sql = "update account set balance=5000 where id=?";int count = template.update(sql, 1);System.out.println(count);} }

    4.2 JDBCTemplate執(zhí)行DML語(yǔ)句

  • 修改1號(hào)數(shù)據(jù)的balance為10000
  • 添加一條記錄
  • 刪除第2步添加的記錄
  • 查詢id為1的記錄,將結(jié)果封裝為map集合
  • 查詢所有記錄,將結(jié)果封裝為list集合
  • 查詢所有記錄,將結(jié)果封裝為Account對(duì)象的list集合
  • 查詢總記錄數(shù)
  • public class JDBCTemplateDemo2 {private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());//更新記錄@Testpublic void test1(){String sql = "update account set balance=? where id=?";int count = template.update(sql,999,1);Assert.assertEquals(1,count);}//添加記錄@Testpublic void test2(){String sql = "insert into account values(null,?,?)";int count = template.update(sql, "趙四", 888);Assert.assertEquals(1,count);}//刪除記錄@Testpublic void test3(){String sql = "delete from account where id = ?";int count = template.update(sql, 4);Assert.assertEquals(1,count);}//查詢id為1的記錄 封裝為map//注意queryForMap查詢的結(jié)果集長(zhǎng)度只能是1@Testpublic void test4(){String sql = "select * from account where id = ?";Map<String, Object> map = template.queryForMap(sql,1);System.out.println(map);}@Test//查詢所有的記錄 封裝到list中 list中是map對(duì)應(yīng)的一條條結(jié)果public void test5(){String sql = "select * from account";List<Map<String, Object>> maps = template.queryForList(sql);System.out.println(maps);}@Test//將結(jié)果封裝為類再存入list中public void test6(){String sql = "select * from account";List<Account> query = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));for(Account acc: query){System.out.println(acc);}}//查詢總記錄數(shù)@Testpublic void test7(){String sql = "select count(id) from account";Long total = template.queryForObject(sql, Long.class);System.out.println(total);} }

    總結(jié)

    以上是生活随笔為你收集整理的【JavaWeb】JDBC优化 之 数据库连接池、Spring JDBC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。