1. /**
  2. * Copyright: Copyright (c) 2005-2005
  3. * Company: JavaResearch(http://www.javaresearch.org)
  4. */
  5. package org.javaresearch.jerch;
  6. import java.lang.reflect.Method;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.ResultSetMetaData;
  11. import java.sql.SQLException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import javax.sql.DataSource;
  15. /**
  16. * JDBC的模板定义,包含所有支持的方法。
  17. * 这个类是这个类库的核心,所有的实际功能差不多都是这个定义并完成的。
  18. * 最后更新日期:2005年5月7日
  19. * @author cherami
  20. */
  21. public class JDBCTemplate {
  22. /**
  23. * 数据源。
  24. */
  25. private DataSource datasource;
  26. /**
  27. * 构造一个空的JDBCTemplate。
  28. */
  29. protected JDBCTemplate() {
  30. }
  31. /**
  32. * 以指定的数据源构造一个JDBCTemplate。
  33. * @param datasource 数据源
  34. */
  35. protected JDBCTemplate(DataSource datasource) {
  36. this.datasource = datasource;
  37. }
  38. /**
  39. * 设置当前JDBCTemplate的数据源。
  40. * @param datasource 数据源
  41. */
  42. protected void setDataSource(DataSource datasource) {
  43. this.datasource = datasource;
  44. }
  45. /**
  46. * 得到当前JDBCTemplate的数据源。
  47. * @return 当前JDBCTemplate的数据源
  48. */
  49. protected DataSource getDataSource() {
  50. return datasource;
  51. }
  52. /**
  53. * 使用psc创建PreparedStatement并使用pss设置相关的参数最后调用action执行。
  54. * @param psc PreparedStatement的创建器
  55. * @param pss PreparedStatement的参数设置器
  56. * @param action PreparedStatement执行的回掉实现
  57. * @return 执行后的结果
  58. */
  59. public Object execute(PreparedStatementCreator psc,
  60. PreparedStatementSetter pss, PreparedStatementCallback action) {
  61. Connection con = Utils.getConnection(datasource);
  62. PreparedStatement ps = null;
  63. try {
  64. ps = psc.createPreparedStatement(con);
  65. pss.setValues(ps);
  66. Object result = action.doInPreparedStatement(ps);
  67. return result;
  68. }
  69. catch (SQLException ex) {
  70. Utils.error("JDBCTemplate.EXECUTE_ERROR", ex);
  71. throw new JerchException("JDBCTemplate.EXECUTE_ERROR",ex);
  72. } finally {
  73. Utils.closeStatement(ps);
  74. Utils.closeConnection(con);
  75. }
  76. }
  77. /**
  78. * 执行指定的sql并返回更新的记录数。
  79. * @param sql SQL语句
  80. * @return 更新的记录数
  81. */
  82. public int update(String sql) {
  83. return update(sql, nullPSS);
  84. }
  85. /**
  86. * 执行指定的sql并返回更新的记录数。
  87. * @param sql SQL语句
  88. * @param args 参数中的值
  89. * @return 更新的记录数
  90. */
  91. public int update(String sql, Object[] args) {
  92. return update(sql, new ArgPreparedStatementSetter(args));
  93. }
  94. /**
  95. * 执行指定的sql并返回更新的记录数。
  96. * @param sql SQL语句
  97. * @param args 参数中的值
  98. * @param types 参数类型
  99. * @return 更新的记录数
  100. */
  101. public int update(String sql, Object[] args, int[] types) {
  102. return update(sql, new ArgTypePreparedStatementSetter(args, types));
  103. }
  104. /**
  105. * 执行指定的sql并返回更新的记录数。
  106. * @param sql SQL语句
  107. * @param pss PreparedStatement的参数设置器
  108. * @return 更新的记录数
  109. */
  110. public int update(String sql, PreparedStatementSetter pss) {
  111. Integer result = (Integer) execute(new SimplePreparedStatementCreator(sql),
  112. pss, new PreparedStatementCallback() {
  113. public Object doInPreparedStatement(PreparedStatement stmt)
  114. throws SQLException {
  115. return new Integer(stmt.executeUpdate());
  116. }
  117. });
  118. return result.intValue();
  119. }
  120. /**
  121. * 批量执行更新并返回每次的更新记录数
  122. * @param sql SQL语句
  123. * @param args 每次执行时的参数值
  124. * @return 每次执行更新的记录数数组
  125. */
  126. public int[] batchUpdate(String sql, Object[][] args) {
  127. return batchUpdate(sql, new ArgBatchPreparedStatementSetter(args));
  128. }
  129. /**
  130. * 批量执行更新并返回每次的更新记录数
  131. * @param sql SQL语句
  132. * @param args 每次执行时的参数值
  133. * @param types 参数类型
  134. * @return 每次执行更新的记录数数组
  135. */
  136. public int[] batchUpdate(String sql, Object[][] args, int[] types) {
  137. return batchUpdate(sql,
  138. new ArgTypeBatchPreparedStatementSetter(args, types));
  139. }
  140. /**
  141. * 批量执行更新并返回每次的更新记录数
  142. * @param sql SQL语句
  143. * @param bpss PreparedStatement的批量参数设置器
  144. * @return 每次执行更新的记录数数组
  145. */
  146. public int[] batchUpdate(String sql, BatchPreparedStatementSetter bpss) {
  147. return batchUpdate(sql, new BatchPreparedStatementSetterConverter(bpss));
  148. }
  149. /**
  150. * 批量执行更新并返回每次的更新记录数
  151. * @param sql SQL语句
  152. * @param pss PreparedStatement的参数设置器
  153. * @return 每次执行更新的记录数数组
  154. */
  155. public int[] batchUpdate(String sql, PreparedStatementSetter pss) {
  156. return (int[]) execute(new SimplePreparedStatementCreator(sql), pss,
  157. new PreparedStatementCallback() {
  158. public Object doInPreparedStatement(PreparedStatement stmt)
  159. throws SQLException {
  160. return stmt.executeBatch();
  161. }
  162. });
  163. }
  164. /**
  165. * 查询一个整型结果。
  166. * @param sql SQL语句
  167. * @return 查询的第一行的第一个字段的整型值。
  168. */
  169. public int queryForInt(String sql) {
  170. return queryForInt(sql, nullPSS);
  171. }
  172. /**
  173. * 查询一个整型结果。
  174. * @param sql SQL语句
  175. * @param args 参数中的值
  176. * @return 查询的第一行的第一个字段的整型值。
  177. */
  178. public int queryForInt(String sql, Object[] args) {
  179. return queryForInt(sql, new ArgPreparedStatementSetter(args));
  180. }
  181. /**
  182. * 查询一个整型结果。
  183. * @param sql SQL语句
  184. * @param args 参数中的值
  185. * @param types 参数类型
  186. * @return 查询的第一行的第一个字段的整型值。
  187. */
  188. public int queryForInt(String sql, Object[] args, int[] types) {
  189. return queryForInt(sql, new ArgTypePreparedStatementSetter(args, types));
  190. }
  191. /**
  192. * 查询一个整型结果。
  193. * @param sql SQL语句
  194. * @param pss PreparedStatement的参数设置器
  195. * @return 查询的第一行的第一个字段的整型值。
  196. */
  197. public int queryForInt(String sql, PreparedStatementSetter pss) {
  198. Number result = (Number) queryObject(sql, pss,
  199. new ObjectResultRowExtractor());
  200. if (result==null) {
  201. return 0;
  202. }
  203. return result.intValue();
  204. }
  205. /**
  206. * 查询一个长整型结果。
  207. * @param sql SQL语句
  208. * @return 查询的第一行的第一个字段的长整型值。
  209. */
  210. public long queryForLong(String sql) {
  211. return queryForLong(sql, nullPSS);
  212. }
  213. /**
  214. * 查询一个长整型结果。
  215. * @param sql SQL语句
  216. * @param args 参数中的值
  217. * @return 查询的第一行的第一个字段的长整型值。
  218. */
  219. public long queryForLong(String sql, Object[] args) {
  220. return queryForLong(sql, new ArgPreparedStatementSetter(args));
  221. }
  222. /**
  223. * 查询一个长整型结果。
  224. * @param sql SQL语句
  225. * @param args 参数中的值
  226. * @param types 参数类型
  227. * @return 查询的第一行的第一个字段的长整型值。
  228. */
  229. public long queryForLong(String sql, Object[] args, int[] types) {
  230. return queryForLong(sql, new ArgTypePreparedStatementSetter(args, types));
  231. }
  232. /**
  233. * 查询一个长整型结果。
  234. * @param sql SQL语句
  235. * @param pss PreparedStatement的参数设置器
  236. * @return 查询的第一行的第一个字段的长整型值。
  237. */
  238. public long queryForLong(String sql, PreparedStatementSetter pss) {
  239. Number result = (Number) queryObject(sql, pss,
  240. new ObjectResultRowExtractor());
  241. if (result==null) {
  242. return 0;
  243. }
  244. return result.longValue();
  245. }
  246. /**
  247. * 查询一个字符串结果。
  248. * @param sql SQL语句
  249. * @return 查询的第一行的第一个字段的字符串值。
  250. */
  251. public String queryString(String sql) {
  252. return queryString(sql, nullPSS);
  253. }
  254. /**
  255. * 查询一个字符串结果。
  256. * @param sql SQL语句
  257. * @param args 参数中的值
  258. * @return 查询的第一行的第一个字段的字符串值。
  259. */
  260. public String queryString(String sql, Object[] args) {
  261. return queryString(sql, new ArgPreparedStatementSetter(args));
  262. }
  263. /**
  264. * 查询一个字符串结果。
  265. * @param sql SQL语句
  266. * @param args 参数中的值
  267. * @param types 参数类型
  268. * @return 查询的第一行的第一个字段的字符串值。
  269. */
  270. public String queryString(String sql, Object[] args, int[] types) {
  271. return queryString(sql, new ArgTypePreparedStatementSetter(args, types));
  272. }
  273. /**
  274. * 查询一个字符串结果。
  275. * @param sql SQL语句
  276. * @param pss PreparedStatement的参数设置器
  277. * @return 查询的第一行的第一个字段的字符串值。
  278. */
  279. public String queryString(String sql, PreparedStatementSetter pss) {
  280. return (String) queryObject(sql, pss, new StringResultRowExtractor());
  281. }
  282. /**
  283. * 查询一个对象结果。
  284. * @param sql SQL语句
  285. * @param rre 单行结果提取器
  286. * @return 查询的第一行的结果并使用rre转换为结果对象。
  287. */
  288. public Object queryObject(String sql, ResultRowExtractor rre) {
  289. return queryObject(sql, nullPSS, rre);
  290. }
  291. /**
  292. * 查询一个对象结果。
  293. * @param sql SQL语句
  294. * @param args 参数中的值
  295. * @param rre 单行结果提取器
  296. * @return 查询的第一行的结果并使用rre转换为结果对象。
  297. */
  298. public Object queryObject(String sql, Object[] args, ResultRowExtractor rre) {
  299. return queryObject(sql, new ArgPreparedStatementSetter(args), rre);
  300. }
  301. /**
  302. * 查询一个对象结果。
  303. * @param sql SQL语句
  304. * @param args 参数中的值
  305. * @param types 参数类型
  306. * @param rre 单行结果提取器
  307. * @return 查询的第一行的结果并使用rre转换为结果对象。
  308. */
  309. public Object queryObject(String sql, Object[] args, int[] types,
  310. ResultRowExtractor rre) {
  311. return queryObject(sql, new ArgTypePreparedStatementSetter(args, types),
  312. rre);
  313. }
  314. /**
  315. * 查询一个对象结果。
  316. * @param sql SQL语句
  317. * @param pss PreparedStatement的参数设置器
  318. * @param rre 单行结果提取器
  319. * @return 查询的第一行的结果并使用rre转换为结果对象。
  320. */
  321. public Object queryObject(String sql, PreparedStatementSetter pss,
  322. final ResultRowExtractor rre) {
  323. Object result = (Object) execute(new SimplePreparedStatementCreator(sql),
  324. pss, new PreparedStatementCallback() {
  325. public Object doInPreparedStatement(PreparedStatement ps)
  326. throws SQLException {
  327. ResultSet rs = null;
  328. try {
  329. rs = ps.executeQuery();
  330. if (rs.next()) {
  331. return rre.extractRow(rs);
  332. } else {
  333. return null;
  334. }
  335. }
  336. catch (SQLException e) {
  337. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  338. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  339. } finally {
  340. Utils.closeResultSet(rs);
  341. }
  342. }
  343. });
  344. return result;
  345. }
  346. /**
  347. * 查询一个对象列表结果。
  348. * @param sql SQL语句
  349. * @param rre 单行结果提取器
  350. * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  351. */
  352. public List query(String sql, ResultRowExtractor rre) {
  353. return (List) execute(new SimplePreparedStatementCreator(sql), nullPSS,
  354. new ResultRowListPreparedStatementCallback(rre));
  355. }
  356. /**
  357. * 查询结果。
  358. * @param sql SQL语句
  359. * @param rse 全部结果提取器。
  360. * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  361. */
  362. public Object query(String sql, final ResultSetExtractor rse) {
  363. return (Object) execute(new SimplePreparedStatementCreator(sql), nullPSS,
  364. new ResultSetPreparedStatementCallback(rse));
  365. }
  366. /**
  367. * 查询一个对象列表结果。
  368. * @param sql SQL语句
  369. * @param args 参数中的值
  370. * @param types 参数类型
  371. * @param rre 单行结果提取器
  372. * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  373. */
  374. public List query(String sql, Object[] args, int[] types, ResultRowExtractor rre) {
  375. return (List) execute(new SimplePreparedStatementCreator(sql),
  376. new ArgTypePreparedStatementSetter(args,types),
  377. new ResultRowListPreparedStatementCallback(rre));
  378. }
  379. /**
  380. * 查询结果。
  381. * @param sql SQL语句
  382. * @param args 参数中的值
  383. * @param types 参数类型
  384. * @param rse 全部结果提取器。
  385. * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  386. */
  387. public Object query(String sql, Object[] args, int[] types, ResultSetExtractor rse) {
  388. return (Object) execute(new SimplePreparedStatementCreator(sql),
  389. new ArgTypePreparedStatementSetter(args,types),
  390. new ResultSetPreparedStatementCallback(rse));
  391. }
  392. /**
  393. * 查询一个对象列表结果。
  394. * @param sql SQL语句
  395. * @param args 参数中的值
  396. * @param rre 单行结果提取器
  397. * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  398. */
  399. public List query(String sql, Object[] args, ResultRowExtractor rre) {
  400. return (List) execute(new SimplePreparedStatementCreator(sql),
  401. new ArgPreparedStatementSetter(args),
  402. new ResultRowListPreparedStatementCallback(rre));
  403. }
  404. /**
  405. * 查询结果。
  406. * @param sql SQL语句
  407. * @param args 参数中的值
  408. * @param rse 全部结果提取器。
  409. * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  410. */
  411. public Object query(String sql, Object[] args, ResultSetExtractor rse) {
  412. return (Object) execute(new SimplePreparedStatementCreator(sql),
  413. new ArgPreparedStatementSetter(args),
  414. new ResultSetPreparedStatementCallback(rse));
  415. }
  416. /**
  417. * 查询一个对象列表结果。
  418. * @param sql SQL语句
  419. * @param pss PreparedStatement的参数设置器
  420. * @param rre 单行结果提取器
  421. * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  422. */
  423. public List query(String sql, PreparedStatementSetter pss,
  424. ResultRowExtractor rre) {
  425. return (List) execute(new SimplePreparedStatementCreator(sql), pss,
  426. new ResultRowListPreparedStatementCallback(rre));
  427. }
  428. /**
  429. * 查询结果。
  430. * @param sql SQL语句
  431. * @param pss PreparedStatement的参数设置器
  432. * @param rse 全部结果提取器。
  433. * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  434. */
  435. public Object query(String sql, PreparedStatementSetter pss,
  436. ResultSetExtractor rse) {
  437. return (Object) execute(new SimplePreparedStatementCreator(sql), pss,
  438. new ResultSetPreparedStatementCallback(rse));
  439. }
  440. /**
  441. * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  442. * @param sql SQL语句
  443. * @param bean 结果Bean的类型
  444. * @return 第一行的结果并转换为Bean实例。
  445. */
  446. public Object queryForBean(String sql, Class bean) {
  447. try {
  448. Object obj = bean.newInstance();
  449. if (obj instanceof Mappable) {
  450. return queryObject(sql, nullPSS, new MappableResultRowExtractor(obj,
  451. (Mappable) obj));
  452. } else {
  453. return queryObject(sql, nullPSS, new MappableResultRowExtractor(obj,
  454. new NameMatchMappable()));
  455. }
  456. }
  457. catch (Exception e) {
  458. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  459. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  460. }
  461. }
  462. /**
  463. * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  464. * @param sql SQL语句
  465. * @param args 参数中的值
  466. * @param bean 结果Bean的类型
  467. * @return 第一行的结果并转换为Bean实例。
  468. */
  469. public Object queryForBean(String sql, Object[] args, Class bean) {
  470. try {
  471. Object obj = bean.newInstance();
  472. if (obj instanceof Mappable) {
  473. return queryObject(sql, new ArgPreparedStatementSetter(args),
  474. new MappableResultRowExtractor(obj, (Mappable) obj));
  475. } else {
  476. return queryObject(sql, new ArgPreparedStatementSetter(args),
  477. new MappableResultRowExtractor(obj, new NameMatchMappable()));
  478. }
  479. }
  480. catch (Exception e) {
  481. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  482. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  483. }
  484. }
  485. /**
  486. * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  487. * @param sql SQL语句
  488. * @param args 参数中的值
  489. * @param types 参数类型
  490. * @param bean 结果Bean的类型
  491. * @return 第一行的结果并转换为Bean实例。
  492. */
  493. public Object queryForBean(String sql, Object[] args, int[] types, Class bean) {
  494. try {
  495. Object obj = bean.newInstance();
  496. if (obj instanceof Mappable) {
  497. return queryObject(sql, new ArgTypePreparedStatementSetter(args,types),
  498. new MappableResultRowExtractor(obj, (Mappable) obj));
  499. } else {
  500. return queryObject(sql, new ArgTypePreparedStatementSetter(args,types),
  501. new MappableResultRowExtractor(obj, new NameMatchMappable()));
  502. }
  503. }
  504. catch (Exception e) {
  505. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  506. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  507. }
  508. }
  509. /**
  510. * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  511. * @param sql SQL语句
  512. * @param pss PreparedStatement的参数设置器
  513. * @param bean 结果Bean的类型
  514. * @return 第一行的结果并转换为Bean实例。
  515. */
  516. public Object queryForBean(String sql, PreparedStatementSetter pss, Class bean) {
  517. try {
  518. Object obj = bean.newInstance();
  519. if (obj instanceof Mappable) {
  520. return queryObject(sql, pss,
  521. new MappableResultRowExtractor(obj, (Mappable) obj));
  522. } else {
  523. return queryObject(sql, pss,
  524. new MappableResultRowExtractor(obj, new NameMatchMappable()));
  525. }
  526. }
  527. catch (Exception e) {
  528. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  529. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  530. }
  531. }
  532. /**
  533. * 查询并返回第一行的结果并使用m将其转换为bean。
  534. * @param sql SQL语句
  535. * @param bean 结果Bean的类型
  536. * @param m 字段-属性映射器
  537. * @return 第一行的结果并使用m将其转换为Bean实例。
  538. */
  539. public Object queryForBean(String sql, Class bean, Mappable m) {
  540. try {
  541. return queryObject(sql, nullPSS, new MappableResultRowExtractor(bean
  542. .newInstance(), m));
  543. }
  544. catch (Exception e) {
  545. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  546. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  547. }
  548. }
  549. /**
  550. * 查询并返回第一行的结果并使用m将其转换为bean。
  551. * @param sql SQL语句
  552. * @param args 参数中的值
  553. * @param bean 结果Bean的类型
  554. * @param m 字段-属性映射器
  555. * @return 第一行的结果并使用m将其转换为Bean实例。
  556. */
  557. public Object queryForBean(String sql, Object[] args, Class bean, Mappable m) {
  558. try {
  559. return queryObject(sql, new ArgPreparedStatementSetter(args),
  560. new MappableResultRowExtractor(bean.newInstance(), m));
  561. }
  562. catch (Exception e) {
  563. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  564. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  565. }
  566. }
  567. /**
  568. * 查询并返回第一行的结果并使用m将其转换为bean。
  569. * @param sql SQL语句
  570. * @param args 参数中的值
  571. * @param types 参数类型
  572. * @param bean 结果Bean的类型
  573. * @param m 字段-属性映射器
  574. * @return 第一行的结果并使用m将其转换为Bean实例。
  575. */
  576. public Object queryForBean(String sql, Object[] args, int[] types, Class bean, Mappable m) {
  577. try {
  578. return queryObject(sql, new ArgTypePreparedStatementSetter(args,types),
  579. new MappableResultRowExtractor(bean.newInstance(), m));
  580. }
  581. catch (Exception e) {
  582. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  583. throw new JerchException("JDBCTemplate.EXECUTE_ERROR", e);
  584. }
  585. }
  586. /**
  587. * 查询并返回第一行的结果并使用m将其转换为bean。
  588. * @param sql SQL语句
  589. * @param pss PreparedStatement的参数设置器
  590. * @param bean 结果Bean的类型
  591. * @param m 字段-属性映射器
  592. * @return 第一行的结果并使用m将其转换为Bean实例。
  593. */
  594. public Object queryForBean(String sql, PreparedStatementSetter pss,
  595. Class bean, Mappable m) {
  596. try {
  597. return queryObject(sql, pss, new MappableResultRowExtractor(bean
  598. .newInstance(), m));
  599. }
  600. catch (Exception e) {
  601. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  602. throw new JerchException("JDBCTemplate.EXECUTE_ERROR",e);
  603. }
  604. }
  605. /**
  606. * 无任何参数的PreparedStatement设置器实例。
  607. */
  608. private static final PreparedStatementSetter nullPSS = new PreparedStatementSetter() {
  609. public void setValues(PreparedStatement ps) {
  610. }
  611. };
  612. /**
  613. * 将参数数组转换为PreparedStatement的设置器的简单适配器。
  614. */
  615. private static class ArgPreparedStatementSetter implements
  616. PreparedStatementSetter {
  617. private final Object[] args;
  618. public ArgPreparedStatementSetter(Object[] args) {
  619. this.args = args;
  620. }
  621. public void setValues(PreparedStatement ps) throws SQLException {
  622. if (this.args != null) {
  623. for (int i = 0; i < this.args.length; i++) {
  624. ps.setObject(i + 1, this.args[i]);
  625. }
  626. }
  627. }
  628. }
  629. /**
  630. * 将参数数组和其类型转换为PreparedStatement的设置器的简单适配器。
  631. */
  632. private static class ArgTypePreparedStatementSetter implements
  633. PreparedStatementSetter {
  634. private final Object[] args;
  635. private final int[] types;
  636. public ArgTypePreparedStatementSetter(Object[] args, int[] types) {
  637. if ((args != null && types == null)
  638. || (args == null && types != null)
  639. || (args != null && args.length != types.length)) {
  640. throw new JerchException("ArgTypePreparedStatementSetter.ARG_NOT_MATCH");
  641. }
  642. this.args = args;
  643. this.types = types;
  644. }
  645. public void setValues(PreparedStatement ps) throws SQLException {
  646. if (this.args != null) {
  647. for (int i = 0; i < this.args.length; i++) {
  648. ps.setObject(i + 1, this.args[i], this.types[i]);
  649. }
  650. }
  651. }
  652. }
  653. /**
  654. * 将sql语句转换为PreparedStatement的创建器的简单适配器。
  655. */
  656. private static class SimplePreparedStatementCreator implements
  657. PreparedStatementCreator {
  658. private final String sql;
  659. public SimplePreparedStatementCreator(String sql) {
  660. this.sql = sql;
  661. }
  662. public PreparedStatement createPreparedStatement(Connection con)
  663. throws SQLException {
  664. return con.prepareStatement(this.sql);
  665. }
  666. }
  667. /**
  668. * 将第一个字段作为String类型提取的单行结果提取器。
  669. */
  670. private static class StringResultRowExtractor implements ResultRowExtractor {
  671. public Object extractRow(java.sql.ResultSet rs) throws SQLException {
  672. return rs.getString(1);
  673. }
  674. }
  675. /**
  676. * 将第一个字段作为Object类型提取的单行结果提取器。
  677. */
  678. private static class ObjectResultRowExtractor implements ResultRowExtractor {
  679. public Object extractRow(java.sql.ResultSet rs) throws SQLException {
  680. return rs.getObject(1);
  681. }
  682. }
  683. /**
  684. * 将BatchPreparedStatement设置其转换为PreparedStatement设置器的适配器。
  685. */
  686. private static class BatchPreparedStatementSetterConverter implements
  687. PreparedStatementSetter {
  688. BatchPreparedStatementSetter bpss;
  689. public BatchPreparedStatementSetterConverter(
  690. BatchPreparedStatementSetter bpss) {
  691. this.bpss = bpss;
  692. }
  693. public void setValues(PreparedStatement ps) throws SQLException {
  694. for (int i = 0; i < bpss.getBatchSize(); i++) {
  695. bpss.setValues(ps, i);
  696. ps.addBatch();
  697. }
  698. }
  699. }
  700. /**
  701. * 将批量参数数组转换为PreparedStatement设置器的适配器。
  702. */
  703. private static class ArgBatchPreparedStatementSetter implements
  704. PreparedStatementSetter {
  705. private final Object[][] args;
  706. public ArgBatchPreparedStatementSetter(Object[][] args) {
  707. this.args = args;
  708. }
  709. public void setValues(PreparedStatement ps) throws SQLException {
  710. if (this.args != null) {
  711. for (int i = 0; i < this.args.length; i++) {
  712. Object[] arg = args[i];
  713. for (int j = 0; j < arg.length; j++) {
  714. ps.setObject(j + 1, arg[j]);
  715. }
  716. ps.addBatch();
  717. }
  718. }
  719. }
  720. }
  721. /**
  722. * 将批量参数数组和对应类型转换为PreparedStatement设置器的适配器。
  723. */
  724. private static class ArgTypeBatchPreparedStatementSetter implements
  725. PreparedStatementSetter {
  726. private final Object[][] args;
  727. private final int[] types;
  728. public ArgTypeBatchPreparedStatementSetter(Object[][] args, int[] types) {
  729. if ((args != null && types == null)
  730. || (args == null && types != null)
  731. || (args != null && args.length != types.length)) {
  732. throw new JerchException("ArgTypePreparedStatementSetter.ARG_NOT_MATCH");
  733. }
  734. this.args = args;
  735. this.types = types;
  736. }
  737. public void setValues(PreparedStatement ps) throws SQLException {
  738. if (this.args != null) {
  739. for (int i = 0; i < this.args.length; i++) {
  740. Object[] arg = args[i];
  741. for (int j = 0; j < arg.length; j++) {
  742. ps.setObject(j + 1, arg[j], types[j]);
  743. }
  744. ps.addBatch();
  745. }
  746. }
  747. }
  748. }
  749. /**
  750. * 使用ResultRowExtractor提取单行结果并将全部结果添加到List的PreparedStatement回调实现。
  751. */
  752. private static class ResultRowListPreparedStatementCallback implements
  753. PreparedStatementCallback {
  754. ResultRowExtractor rre;
  755. public ResultRowListPreparedStatementCallback(ResultRowExtractor rre) {
  756. this.rre = rre;
  757. }
  758. public Object doInPreparedStatement(PreparedStatement ps)
  759. throws SQLException {
  760. ResultSet rs = null;
  761. List result = new ArrayList();
  762. try {
  763. rs = ps.executeQuery();
  764. while (rs.next()) {
  765. result.add(rre.extractRow(rs));
  766. }
  767. }
  768. catch (SQLException e) {
  769. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  770. throw new JerchException("JDBCTemplate.EXECUTE_ERROR",e);
  771. } finally {
  772. Utils.closeResultSet(rs);
  773. }
  774. return result;
  775. }
  776. }
  777. /**
  778. * 使用ResultSetExtractor提取全部结果的PreparedStatement回调实现。
  779. */
  780. private static class ResultSetPreparedStatementCallback implements
  781. PreparedStatementCallback {
  782. ResultSetExtractor rse;
  783. public ResultSetPreparedStatementCallback(ResultSetExtractor rse) {
  784. this.rse = rse;
  785. }
  786. public Object doInPreparedStatement(PreparedStatement ps)
  787. throws SQLException {
  788. ResultSet rs = null;
  789. try {
  790. rs = ps.executeQuery();
  791. return rse.extractSet(rs);
  792. }
  793. catch (SQLException e) {
  794. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  795. throw new JerchException("JDBCTemplate.EXECUTE_ERROR",e);
  796. } finally {
  797. Utils.closeResultSet(rs);
  798. }
  799. }
  800. }
  801. /**
  802. * 使用Mappable将单行结果转换为对象实例的ResultRow提取器实现。
  803. */
  804. private static class MappableResultRowExtractor implements ResultRowExtractor {
  805. private Mappable map;
  806. private Object obj;
  807. public MappableResultRowExtractor(Object obj, Mappable map) {
  808. this.obj = obj;
  809. this.map = map;
  810. }
  811. public Object extractRow(ResultSet rs) throws SQLException {
  812. Class c = obj.getClass();
  813. ResultSetMetaData metaData = rs.getMetaData();
  814. int count = metaData.getColumnCount();
  815. try {
  816. for (int i = 0; i < count; i++) {
  817. String fieldName = metaData.getColumnName(i + 1);
  818. int type = metaData.getColumnType(i + 1);
  819. Class[] types = { map.getMethodParameterType(fieldName, type) };
  820. Method m = c.getMethod(map.getMapMethod(fieldName), types);
  821. Object[] args = { ValueConverterFactory.convert(rs
  822. .getObject(fieldName), types[0]) };
  823. m.invoke(obj, args);
  824. }
  825. }
  826. catch (Exception e) {
  827. Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  828. throw new JerchException("JDBCTemplate.EXECUTE_ERROR",e);
  829. }
  830. return obj;
  831. }
  832. }
  833. }