国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
SpringFramework(7)

4、JDBC

1JDBC概要

l         使JDBC使用更容易,更少出錯(cuò)

l         由框架來處理資源的創(chuàng)建和釋放

l         由框架來管理異常處理

2JdbcTemplate

l         執(zhí)行SQL查詢、更新語(yǔ)句和存儲(chǔ)過程調(diào)用

l         循環(huán)遍歷ResultSet和提取返回的參數(shù)值

l         例子:

DataSource ds = DataSourceUtils.getDataSourceFromJndi("MyDS");
JdbcTemplate jdbc = new JdbcTemplate(ds);
jdbc.execute("drop table TEMP");
jdbc.update("update EMPLOYEE set FIRSTNME=? where LASTNAME=?",
new String[] {"JOE", "LEE"});

l         使用方便方法進(jìn)行查詢

int maxAge = jdbc.queryForInt("select max(AGE) from EMPLOYEE");
String name = (String)jdbc.queryForObject(
"select FIRSTNME from EMPLOYEE where LASTNAME=‘LEE‘", String.class);
List employees = jdbc.queryForList(
"select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE");

返回一個(gè)ArrayList(一個(gè)條目對(duì)應(yīng)一行)的HashMap(一個(gè)條目對(duì)應(yīng)一列,使用列名做key

l         使用回調(diào)方法查詢

final List employees = new LinkedList();
jdbc.query("select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE",
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
Employee e = new Employee();
e.setEmpNo(rs.getString(1));
e.setFirstName(rs.getString(2));
e.setLastName(rs.getString(3));
employees.add(e);
}
});

l         存儲(chǔ)過程

jdbc.call(new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection conn)
throws SQLException {
return conn.prepareCall("my query");
}
}, params);

l         批更新

BatchPreparedStatementSetter setter =
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i)
throws SQLException {
...
}
public int getBatchSize() {
return ...;
}
};
jdbc.batchUpdate("update ...", setter);

3SqlQuery/SqlUpdate對(duì)象

l         封裝查詢和更新到Java類中

class EmployeeQuery extends MappingSqlQuery {
public EmployeeQuery(DataSource ds) {
super(ds, "select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE where EMPNO = ?");
declareParameter(new SqlParameter(Types.CHAR));
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
Employee e = new Employee();
e.setEmpNo(rs.getString("EMPNO"));
e.setFirstName(rs.getString("FIRSTNME"));
e.setLastName(rs.getString("LASTNAME"));
return e;
}
public Employee findEmployee(String id) {
return (Employee) findObject(id);
}
}

映射結(jié)果集的行到一個(gè)Java對(duì)象

4SqlFunction

l         封裝返回單行的查詢

SqlFunction sf = new SqlFunction(dataSource,
"select count(*) from mytable");
sf.compile();
int rows = sf.run();

5)異常處理

l         轉(zhuǎn)換SQLExecptionDataAccessException層面

Ø         通用,更多信息,與DB/JDBC無關(guān)(sql錯(cuò)誤代碼被映射到異常)

l         使用RuntimeException(沒有檢查)

l         我們可以覆蓋未檢查的數(shù)據(jù)訪問異常

try {
// do work
} catch (OptimisticLockingFailureException ex) {
// I‘m interested in this
}

6)數(shù)據(jù)庫(kù)連接

l         DataSourceUtilsgetConnection()、getDatSourceFromJndi()、closeConnectionIfNecessary()

l         DriverManagerDataSource

Ø         每次返回一個(gè)新的連接

Ø         能夠在容器外或測(cè)試中使用

l         SingleConnectionDataSource

Ø         每次返回同一個(gè)連接

Ø         能夠在容器外或測(cè)試中使用

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring里使用JDBC
Spring:JdbcTemplate使用指南
Spring JDBC
JdbcTemplate學(xué)習(xí)筆記
《Java Web應(yīng)用程序開發(fā)》03 JDBC(一)
Spring 的 jdbctemplate 查詢對(duì)象
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服