生活随笔
收集整理的這篇文章主要介紹了
【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 {private static DataSource ds
;static{try {Properties pro
= new Properties();pro
.load(JDBCUtils
.class.getClassLoader().getResourceAsStream("druid.properties"));ds
= 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
);}@Testpublic void test4(){String sql
= "select * from account where id = ?";Map
<String, Object> map
= template
.queryForMap(sql
,1);System
.out
.println(map
);}@Testpublic void test5(){String sql
= "select * from account";List
<Map
<String, Object>> maps
= template
.queryForList(sql
);System
.out
.println(maps
);}@Testpublic 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
);}}@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ò),歡迎將生活随笔推薦給好友。