一、安裝
下載最新的 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
