在Java中使用Sqlite数据库
一、安装
下载最新的 Sqlite Jdbc 驱动程序jar文件,并添加到Java工程的class路径下;
二、使用
以 sqlite Jdbc 驱动版本为 sqlitejdbc-v56.jar 为例
SqliteHelper.java 类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* sqlite帮助类,直接创建该类示例,并调用相应的借口即可对sqlite数据库进行操作
*
* 本类基于 sqlite jdbc v56
*
* @author haoqipeng
*/
public class SqliteHelper {
final static Logger logger = LoggerFactory.getLogger(SqliteHelper.class); private Connection connection;
private Statement statement;
private ResultSet resultSet;
private String dbFilePath; /**
* 构造函数
* @param dbFilePath sqlite db 文件路径
* @throws ClassNotFoundException
* @throws SQLException
*/
public SqliteHelper(String dbFilePath) throws ClassNotFoundException, SQLException {
this.dbFilePath = dbFilePath;
connection = getConnection(dbFilePath);
} /**
* 获取数据库连接
* @param dbFilePath db文件路径
* @return 数据库连接
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection(String dbFilePath) throws ClassNotFoundException, SQLException {
Connection conn = null;
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
return conn;
} /**
* 执行sql查询
* @param sql sql select 语句
* @param rse 结果集处理类对象
* @return 查询结果
* @throws SQLException
* @throws ClassNotFoundException
*/
public <T> T executeQuery(String sql, ResultSetExtractor<T> rse) throws SQLException, ClassNotFoundException {
try {
resultSet = getStatement().executeQuery(sql);
T rs = rse.extractData(resultSet);
return rs;
} finally {
destroyed();
}
} /**
* 执行select查询,返回结果列表
*
* @param sql sql select 语句
* @param rm 结果集的行数据处理类对象
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public <T> List<T> executeQuery(String sql, RowMapper<T> rm) throws SQLException, ClassNotFoundException {
List<T> rsList = new ArrayList<T>();
try {
resultSet = getStatement().executeQuery(sql);
while (resultSet.next()) {
rsList.add(rm.mapRow(resultSet, resultSet.getRow()));
}
} finally {
destroyed();
}
return rsList;
} /**
* 执行数据库更新sql语句
* @param sql
* @return 更新行数
* @throws SQLException
* @throws ClassNotFoundException
*/
public int executeUpdate(String sql) throws SQLException, ClassNotFoundException {
try {
int c = getStatement().executeUpdate(sql);
return c;
} finally {
destroyed();
} } /**
* 执行多个sql更新语句
* @param sqls
* @throws SQLException
* @throws ClassNotFoundException
*/
public void executeUpdate(String...sqls) throws SQLException, ClassNotFoundException {
try {
for (String sql : sqls) {
getStatement().executeUpdate(sql);
}
} finally {
destroyed();
}
} /**
* 执行数据库更新 sql List
* @param sqls sql列表
* @throws SQLException
* @throws ClassNotFoundException
*/
public void executeUpdate(List<String> sqls) throws SQLException, ClassNotFoundException {
try {
for (String sql : sqls) {
getStatement().executeUpdate(sql);
}
} finally {
destroyed();
}
} private Connection getConnection() throws ClassNotFoundException, SQLException {
if (null == connection) connection = getConnection(dbFilePath);
return connection;
} private Statement getStatement() throws SQLException, ClassNotFoundException {
if (null == statement) statement = getConnection().createStatement();
return statement;
} /**
* 数据库资源关闭和释放
*/
public void destroyed() {
try {
if (null != connection) {
connection.close();
connection = null;
} if (null != statement) {
statement.close();
statement = null;
} if (null != resultSet) {
resultSet.close();
resultSet = null;
}
} catch (SQLException e) {
logger.error("Sqlite数据库关闭时异常", e);
}
}
}
ResltSetExtractor.java 结果集处理类
import java.sql.ResultSet;
public interface ResultSetExtractor<T> {
public abstract T extractData(ResultSet rs);
}
RowMapper.java 结果集行数据处理类
import java.sql.ResultSet;
import java.sql.SQLException; public interface RowMapper<T> {
public abstract T mapRow(ResultSet rs, int index) throws SQLException;
}
SqliteTest.java 测试类
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; import org.junit.Test; public class SqliteTest { @Test
public void testHelper() {
try {
SqliteHelper h = new SqliteHelper("testHelper.db");
h.executeUpdate("drop table if exists test;");
h.executeUpdate("create table test(name varchar(20));");
h.executeUpdate("insert into test values('sqliteHelper test');");
List<String> sList = h.executeQuery("select name from test", new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int index)
throws SQLException {
return rs.getString("name");
}
});
System.out.println(sList.get(0));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
测试输出结果
sqliteHelper test
在Java中使用Sqlite数据库的更多相关文章
- 在Java中使用SQLite的教程(转)
简介:这是在Java中使用SQLite的教程的详细页面,介绍了和java,有关的知识.技巧.经验,和一些java源码等. 简单的在Java中使用SQLite的教程 使用SQLiteJDBC封装 www ...
- 在 Android 应用程序中使用 SQLite 数据库以及怎么用
part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...
- Android虚拟机中的sqlite数据库文件
Android虚拟机中的sqlite数据库文件 ①
- 在项目中使用SQLite数据库小结
------------------------------------------------------------------------推荐: - VS2012 使用 1.0.84 版的库 - ...
- 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET
今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...
- android中与SQLite数据库相关的类
为什么要在应用程序中使用数据库?数据库最主要的用途就是作为数据的存储容器,另外,由于可以很方便的将应用程序中的数据结构(比如C语言中的结构体)转化成数据库的表,这样我们就可以通过操作数据库来替代写一堆 ...
- Go语言中使用SQLite数据库
Go语言中使用SQLite数据库 1.驱动 Go支持sqlite的驱动也比较多,但是好多都是不支持database/sql接口的 https://github.com/mattn/go-sqlite3 ...
- 在Android 开发中使用 SQLite 数据库笔记
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...
- Java中连接MySql数据库的例子
Java中连接MySql数据库的例子: package com.joinmysql.demo; import java.sql.DriverManager; import java.sql.Resul ...
随机推荐
- 关于C#泛型作用的简单说明
泛型:即通过参数化类型来实现在同一份代码上操作多种数据类型.泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用. C#泛型的作用概述 C#泛型赋予了代码更强的类型安全,更 ...
- [APP] Android 开发笔记 006-使用短信验证SDK进行短信验证
1. 下载SDK (http://www.mob.com/#/download) SMS For Android Studio: http://www.mob.com/download/sms/and ...
- PyCharm导入tensorflow包报错的问题
[注]PyCharm导入tensorflow包报错的问题 若是你也遇到这个问题,说明你也没有理解tensorflow到底在哪里. 当安装了anaconda3.6后,在PyCharm中设置interpr ...
- Pymongo NotMasterError while fetching count of the collection as per query from MongoDB in DRF
django rest framework - Pymongo NotMasterError while fetching count of the collection as per query f ...
- 洛谷P3599 Koishi Loves Construction 构造
正解:构造 解题报告: 传送门! 这题俩问嘛,就分成两个问题港QwQ 就按顺序趴,先港第一问QwQ 首先要发现,n在膜n意义下就是0嘛 那作为前缀和的话显然它就只能放在第一个 然后再想下,发现,如果n ...
- Why String is Immutable or Final in Java
The string is Immutable in Java because String objects are cached in String pool. Since cached Strin ...
- Looper分析。ThreadLocal有关
Class used to run a message loop for a thread. Threads by default do not have a message loop associa ...
- form表单的enctype属性
①application/x-www-form-urlencoded : 数据被编码为名称/值对. ②multipart/form-data : 数据被编码为一条消息,页上的每个控件对应消息中的一个部 ...
- js_加入收藏夹功能
<script type="text/javascript">function addToFavorite(obj) { var url = "http ...
- 前端 HTML标签介绍
那什么是HTML标签呢? 1. 在HTML中规定标签使用英文的的尖括号即"<"和">"包起来,如`<html>`.`<p>` ...