使用dbcp连接池+sqlite
关于datasource如何配置请参看上一篇文章,谢谢
发布日期:2022-02-10 11:36:49
浏览次数:27
分类:技术文章
本文共 5419 字,大约阅读时间需要 18 分钟。
尊重版权:本文绝大部分来自: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上,比如设置自动回滚
- package com.search.util;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.apache.commons.dbcp.BasicDataSource;
- /**
- * 连接和使用数据库资源的工具类
- */
- public class DatabaseSqliteUtil {
- /**
- * 数据源
- */
- private BasicDataSource dataSource;
- /**
- * 数据库连接
- */
- public Connection conn;
- /**
- * 获取数据源
- * @return 数据源
- */
- public BasicDataSource getDataSource() {
- return dataSource;
- }
- /**
- * 设置数据源
- * @param dataSource 数据源
- */
- public void setDataSource(BasicDataSource dataSource) {
- this.dataSource = dataSource;
- }
- /**
- * 获取数据库连接
- * @return conn
- */
- public Connection getConnection() {
- try {
- conn = dataSource.getConnection();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- return conn;
- }
- /**
- * 关闭数据库连接
- * @param conn
- */
- public static void closeConnection(Connection conn) {
- if (null != conn) {
- try {
- conn.close();
- conn = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获取执行SQL的工具
- * @param conn 数据库连接
- * @return stmt
- */
- public static int getFoundRows(Connection conn) {
- Statement stmt=null;
- ResultSet rs=null;
- try {
- stmt=getStatement(conn);
- rs=stmt.executeQuery("SELECT FOUND_ROWS()");
- if(rs.next()){
- return rs.getInt(1);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- closeStatement(stmt);
- closeResultSet(rs);
- }
- return 0;
- }
- /**
- * 获取执行SQL的工具
- * @param conn 数据库连接
- * @return stmt
- */
- public static Statement getStatement(Connection conn) {
- Statement stmt = null;
- try {
- stmt = conn.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return stmt;
- }
- /**
- * 获取执行SQL的工具
- * @param conn 数据库连接
- * @param sql SQL语句
- * @return prepStmt
- */
- public static PreparedStatement getPrepStatement(Connection conn, String sql) {
- PreparedStatement prepStmt = null;
- try {
- prepStmt = conn.prepareStatement(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return prepStmt;
- }
- /**
- * 关闭数据库资源
- * @param stmt
- */
- public static void closeStatement(Statement stmt) {
- if (null != stmt) {
- try {
- stmt.close();
- stmt = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 关闭数据库资源
- * @param prepStmt
- */
- public static void closePrepStatement(PreparedStatement prepStmt) {
- if (null != prepStmt) {
- try {
- prepStmt.close();
- prepStmt = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获取结果集
- * @param stmt 执行SQL的工具
- * @param sql SQL语句
- * @return 结果集
- */
- public static ResultSet getResultSet(Statement stmt, String sql) {
- ResultSet rs = null;
- try {
- rs = stmt.executeQuery(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rs;
- }
- /**
- * 关闭数据库资源
- * @param rs
- */
- public static void closeResultSet(ResultSet rs) {
- if (null != rs) {
- try {
- rs.close();
- rs = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static Boolean setAutoCommit(Connection conn,boolean commitStatus){
- if(conn==null){
- return true;
- }
- try {
- conn.setAutoCommit(commitStatus);
- boolean commit = conn.getAutoCommit();
- return commit;
- } catch (SQLException e1) {
- e1.printStackTrace();
- return true;
- }
- }
- public static boolean rollback(Connection conn,boolean oldCommitStatus){
- if(conn==null){
- return true;
- }
- try {
- conn.setAutoCommit(oldCommitStatus);
- conn.rollback(); // 事物回滚
- return true;
- } catch (SQLException e1) {
- e1.printStackTrace();
- return false;
- }
- }
- public static boolean commit(Connection conn,boolean oldCommitStatus){
- if(conn==null){
- return true;
- }
- try {
- conn.setAutoCommit(oldCommitStatus);
- conn.commit(); // 事物提交
- return true;
- } catch (SQLException e1) {
- e1.printStackTrace();
- return false;
- }
- }
- public static int getLastId(PreparedStatement ps){
- ResultSet rs=null;
- try {
- rs = ps.getGeneratedKeys();
- if (rs != null&&rs.next()) {
- return rs.getInt(1);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- closeResultSet(rs);
- }
- return -1;
- }
- /**
- * 判断是否是管理员
- * @param conn mysql连接
- * @param ip 请求ip
- * @param password 管理员密码
- * @author yaofuyuan
- * @since 2011-08-02 12:58:00
- * @return void 0:不是,1:是,-1:数据库出错
- */
- public int isSuperAdmin(Connection conn,String ip,String password){
- if(conn==null){
- return -1;
- }
- PreparedStatement ps =getPrepStatement(
- conn,
- "select count(*) as count from auth_admin_server where ip=? and password=?");
- ResultSet rs = null;
- try {
- // 查询帐号,用户名和密码
- ps.setString(1, ip);
- ps.setString(2, password);
- rs=ps.executeQuery();
- if (rs.next()) {
- if(rs.getInt("count")==0){
- //用户名密码错误
- return 0;
- }else{
- return 1;
- }
- }
- return -1;
- }
- catch(Exception e){
- e.printStackTrace();
- return -1;
- }finally{
- closeResultSet(rs);
- closePrepStatement(ps);
- }
- }
- public int test(Connection conn){
- PreparedStatement pst =getPrepStatement(conn, "select 123");
- // 获取结果集
- ResultSet rs = null;
- try {
- rs = pst.executeQuery();
- if (rs.next()) {
- return rs.getInt(1);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- // 关闭数据库资源
- closeResultSet(rs);
- closePrepStatement(pst);
- }
- return -1;
- }
- }
转载地址:https://blog.csdn.net/courage89/article/details/8791327 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月07日 03时08分40秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java Web上传数据图片文件
2019-04-26
Java Web 网络商城案例演示二十一 利用工厂模式解耦(适用于不同数据库的配置)
2019-04-26
Java Web 网络商城案例演示二十二 管理员查询订单
2019-04-26
Java Web 网络商城案例演示二十三 管理员界面异步显示订单详情 修改订单状态
2019-04-26
Hibernate框架介绍以及入门 【一】Hibernate 快速入门 框架的概述 什么是 Hibernate Hibernate 持久层的ORM框架 下载Hibernate 官网 编写测试代码
2019-04-26
10分钟入门Hibernate的一级缓存
2019-04-26
10分钟解决Hibernate的事务管理,Hibernate当中要设置事务的隔离级别
2019-04-26
Hibernate一对多的关联映射,详解(代码+图解)与应用 举个栗子,搞的清楚
2019-04-26
一张图快速理解入门递归函数(Java版)
2019-04-26
C语言算法 求最大公约数(辗转相除法)
2019-04-26
掌握C语言判断素数(质数)相关内容
2019-04-26
小米多看电纸书 安装悬浮球 安装桌面 安装微信阅读 小米多看电纸书悬浮球
2019-04-26
Java Web 判断访问的客户端是手机还是电脑(区分安卓,iPhone,pc)
2019-04-26
Hibernate多对多的关系映射,详解(代码+图解)与应用 举个栗子,搞的清楚
2019-04-26
Hibernate环境搭建
2019-04-26
Struts2快速入门,超简单详细的快速入门教程
2019-04-26