使用dbcp连接池+sqlite


尊重版权:本文绝大部分来自:http://blog.chinaunix.net/uid-11121450-id-3129713.html

1、准备并导入jar包

commons-pool.jar
commons-dbcp.jar
sqlite-jdbc-3.7.2.jar

其在maven中配置文件为:

            <dependency>
                <groupId>org.xerial</groupId>
                <artifactId>sqlite-jdbc</artifactId>
                <version>3.7.2</version>

            </dependency>

2、几个基本操作

其基本思路如下:

(1)获得datasource

(2)根据datasource获得connection

(3)根据connection获得statement或者根据connection+sql语句获得preparedStatement

(4)执行statement+sql或者preparedStatement得到返回结果resultSet

(5)根据ResultSet得到需要的返回值

(6)关闭ResultSet

(7)关闭statement或者preparedStatement

(8)关闭Connection

注意:所有关于设置数据库属性的操作全部在connection上,比如设置自动回滚


  1. package com.search.util;

  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;


  7. import org.apache.commons.dbcp.BasicDataSource;


  8. /**
  9. * 连接和使用数据库资源的工具类
  10. */
  11. public class DatabaseSqliteUtil {

  12. /**
  13. * 数据源
  14. */
  15. private BasicDataSource dataSource;

  16. /**
  17. * 数据库连接
  18. */
  19. public Connection conn;

  20. /**
  21. * 获取数据源
  22. * @return 数据源
  23. */
  24. public BasicDataSource getDataSource() {
  25. return dataSource;
  26. }

  27. /**
  28. * 设置数据源
  29. * @param dataSource 数据源
  30. */
  31. public void setDataSource(BasicDataSource dataSource) {
  32. this.dataSource = dataSource;
  33. }


  34. /**
  35. * 获取数据库连接
  36. * @return conn
  37. */
  38. public Connection getConnection() {
  39. try {
  40. conn = dataSource.getConnection();
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. return null;
  44. }
  45. return conn;
  46. }

  47. /**
  48. * 关闭数据库连接
  49. * @param conn
  50. */
  51. public static void closeConnection(Connection conn) {
  52. if (null != conn) {
  53. try {
  54. conn.close();
  55. conn = null;
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }

  61. /**
  62. * 获取执行SQL的工具
  63. * @param conn 数据库连接
  64. * @return stmt
  65. */
  66. public static int getFoundRows(Connection conn) {
  67. Statement stmt=null;
  68. ResultSet rs=null;
  69. try {
  70. stmt=getStatement(conn);
  71. rs=stmt.executeQuery("SELECT FOUND_ROWS()");
  72. if(rs.next()){
  73. return rs.getInt(1);
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }finally{
  78. closeStatement(stmt);
  79. closeResultSet(rs);
  80. }
  81. return 0;
  82. }

  83. /**
  84. * 获取执行SQL的工具
  85. * @param conn 数据库连接
  86. * @return stmt
  87. */
  88. public static Statement getStatement(Connection conn) {
  89. Statement stmt = null;
  90. try {
  91. stmt = conn.createStatement();
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. }

  95. return stmt;
  96. }

  97. /**
  98. * 获取执行SQL的工具
  99. * @param conn 数据库连接
  100. * @param sql SQL语句
  101. * @return prepStmt
  102. */
  103. public static PreparedStatement getPrepStatement(Connection conn, String sql) {
  104. PreparedStatement prepStmt = null;
  105. try {
  106. prepStmt = conn.prepareStatement(sql);
  107. } catch (SQLException e) {
  108. e.printStackTrace();
  109. }
  110. return prepStmt;
  111. }

  112. /**
  113. * 关闭数据库资源
  114. * @param stmt
  115. */
  116. public static void closeStatement(Statement stmt) {
  117. if (null != stmt) {
  118. try {
  119. stmt.close();
  120. stmt = null;
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }

  126. /**
  127. * 关闭数据库资源
  128. * @param prepStmt
  129. */
  130. public static void closePrepStatement(PreparedStatement prepStmt) {
  131. if (null != prepStmt) {
  132. try {
  133. prepStmt.close();
  134. prepStmt = null;
  135. } catch (SQLException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. }

  140. /**
  141. * 获取结果集
  142. * @param stmt 执行SQL的工具
  143. * @param sql SQL语句
  144. * @return 结果集
  145. */
  146. public static ResultSet getResultSet(Statement stmt, String sql) {
  147. ResultSet rs = null;
  148. try {
  149. rs = stmt.executeQuery(sql);
  150. } catch (SQLException e) {
  151. e.printStackTrace();
  152. }
  153. return rs;
  154. }

  155. /**
  156. * 关闭数据库资源
  157. * @param rs
  158. */
  159. public static void closeResultSet(ResultSet rs) {
  160. if (null != rs) {
  161. try {
  162. rs.close();
  163. rs = null;
  164. } catch (SQLException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169. public static Boolean setAutoCommit(Connection conn,boolean commitStatus){
  170. if(conn==null){
  171. return true;
  172. }
  173. try {
  174. conn.setAutoCommit(commitStatus);
  175. boolean commit = conn.getAutoCommit();
  176. return commit;
  177. } catch (SQLException e1) {
  178. e1.printStackTrace();
  179. return true;
  180. }
  181. }
  182. public static boolean rollback(Connection conn,boolean oldCommitStatus){
  183. if(conn==null){
  184. return true;
  185. }
  186. try {
  187. conn.setAutoCommit(oldCommitStatus);
  188. conn.rollback(); // 事物回滚

  189. return true;
  190. } catch (SQLException e1) {
  191. e1.printStackTrace();
  192. return false;
  193. }
  194. }
  195. public static boolean commit(Connection conn,boolean oldCommitStatus){
  196. if(conn==null){
  197. return true;
  198. }
  199. try {
  200. conn.setAutoCommit(oldCommitStatus);
  201. conn.commit(); // 事物提交
  202. return true;
  203. } catch (SQLException e1) {
  204. e1.printStackTrace();
  205. return false;
  206. }
  207. }
  208. public static int getLastId(PreparedStatement ps){
  209. ResultSet rs=null;
  210. try {
  211. rs = ps.getGeneratedKeys();
  212. if (rs != null&&rs.next()) {
  213. return rs.getInt(1);
  214. }
  215. } catch (SQLException e) {
  216. // TODO Auto-generated catch block
  217. e.printStackTrace();

  218. }finally{
  219. closeResultSet(rs);
  220. }
  221. return -1;
  222. }
  223. /**
  224. * 判断是否是管理员
  225. * @param conn mysql连接
  226. * @param ip 请求ip
  227. * @param password 管理员密码
  228. * @author yaofuyuan
  229. * @since 2011-08-02 12:58:00
  230. * @return void 0:不是,1:是,-1:数据库出错
  231. */
  232. public int isSuperAdmin(Connection conn,String ip,String password){
  233. if(conn==null){
  234. return -1;
  235. }
  236. PreparedStatement ps =getPrepStatement(
  237. conn,
  238. "select count(*) as count from auth_admin_server where ip=? and password=?");
  239. ResultSet rs = null;
  240. try {
  241. // 查询帐号,用户名和密码
  242. ps.setString(1, ip);
  243. ps.setString(2, password);
  244. rs=ps.executeQuery();
  245. if (rs.next()) {
  246. if(rs.getInt("count")==0){
  247. //用户名密码错误
  248. return 0;
  249. }else{
  250. return 1;
  251. }
  252. }
  253. return -1;
  254. }
  255. catch(Exception e){
  256. e.printStackTrace();
  257. return -1;
  258. }finally{
  259. closeResultSet(rs);
  260. closePrepStatement(ps);
  261. }

  262. }


  263. public int test(Connection conn){

  264. PreparedStatement pst =getPrepStatement(conn, "select 123");
  265. // 获取结果集
  266. ResultSet rs = null;
  267. try {
  268. rs = pst.executeQuery();
  269. if (rs.next()) {
  270. return rs.getInt(1);
  271. }
  272. } catch (SQLException e) {
  273. e.printStackTrace();
  274. } finally {
  275. // 关闭数据库资源
  276. closeResultSet(rs);
  277. closePrepStatement(pst);
  278. }

  279. return -1;
  280. }


  281. }
关于datasource如何配置请参看上一篇文章,谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值