Using JDBC to Operate SQLite in Java
Introduction to SQLite SQLite is a lightweight database system that can be used without installation, and can also be very easily embedded into a system. FireFox embeds SQLite, so you can directly use JavaScript on FireFox to call and operate the database. SQLite is written in C, can cross platforms such as Linux and Windows, and for access and operations in Java, JDBC can be used to connect to SQLite. For connecting to SQLite with JDBC, there are roughly two approaches, one is
目录4 个章节
Introduction to SQLite
SQLite is a lightweight database system that can be used without installation, and can also be very easily embedded into a system. FireFox embeds SQLite, so you can directly use JavaScript on FireFox to call and operate the database.
SQLite is written in C, can cross platforms such as Linux and Windows, and for access and operations in Java, JDBC can be used to connect to SQLite.
For connecting to SQLite with JDBC, there are roughly two approaches: one is connecting to the data with Pure-Java, and the other is directly using Java to call a library written in C. In actual tests, the approach of directly calling the C library should be faster, but on platforms where a suitable C library cannot be found, the Pure-Java version can be used.
SQLiteJDBC this version currently found is more actively updated, and in usage it is almost the same as ordinary JDBC, so getting started with the program is very simple.
SQLiteJDBC can be downloaded from here. The following examples were made with version 3.5.7, providing examples of creating a Table, removing a Table, querying, inserting, deleting, and updating.
Example Code
import java.sql.*;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;
public class MyTest {
public Connection getConnection() throws SQLException
{
SQLiteConfig config = new SQLiteConfig();
// config.setReadOnly(true);
config.setSharedCache(true);
config.enableRecursiveTriggers(true);
SQLiteDataSource ds = new SQLiteDataSource(config);
ds.setUrl("jdbc:sqlite:sample.db");
return ds.getConnection();
//ds.setServerName("sample.db");
}
//create Table
public void createTable(Connection con )throws SQLException{
String sql = "DROP TABLE IF EXISTS test ;create table test (id integer, name string); ";
Statement stat = null;
stat = con.createStatement();
stat.executeUpdate(sql);
}
//drop table
public void dropTable(Connection con)throws SQLException{
String sql = "drop table test ";
Statement stat = null;
stat = con.createStatement();
stat.executeUpdate(sql);
}
//新增
public void insert(Connection con,int id,String name)throws SQLException{
String sql = "insert into test (id,name) values(?,?)";
PreparedStatement pst = null;
pst = con.prepareStatement(sql);
int idx = 1 ;
pst.setInt(idx++, id);
pst.setString(idx++, name);
pst.executeUpdate();
}
//修改
public void update(Connection con,int id,String name)throws SQLException{
String sql = "update test set name = ? where id = ?";
PreparedStatement pst = null;
pst = con.prepareStatement(sql);
int idx = 1 ;
pst.setString(idx++, name);
pst.setInt(idx++, id);
pst.executeUpdate();
}
//刪除
public void delete(Connection con,int id)throws SQLException{
String sql = "delete from test where id = ?";
PreparedStatement pst = null;
pst = con.prepareStatement(sql);
int idx = 1 ;
pst.setInt(idx++, id);
pst.executeUpdate();
}
public void selectAll(Connection con)throws SQLException{
String sql = "select * from test";
Statement stat = null;
ResultSet rs = null;
stat = con.createStatement();
rs = stat.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt("id")+"\t"+rs.getString("name"));
}
}
public static void main(String args[]) throws SQLException{
MyTest test = new MyTest();
Connection con = test.getConnection();
//建立table
test.createTable(con);
//新增資料
test.insert(con, 1, "第一個");
test.insert(con, 2, "第二個");
//查詢顯示資料
System.out.println("新增二筆資料後狀況:");
test.selectAll(con);
//修改資料
System.out.println("修改第一筆資料後狀況:");
test.update(con, 1, "這個值被改變了!");
//查詢顯示資料
test.selectAll(con);
//刪除資料
System.out.println("刪除第一筆資料後狀況:");
test.delete(con, 1);
//查詢顯示資料
test.selectAll(con);
//刪除table
test.dropTable(con);
con.close();
}
}
Explanation
SQLite does not have a user login mechanism, so you only need to specify the location of the database file to access in order to use it. In jdbc:sqlite:sample.db, sample.db is the file name; you can also specify its path location c:\sample.db.
The following is the execution result:

The sample.db file, which did not originally exist, was also created automatically, under the project directory.

References
_EOF_
Using JDBC to Operate SQLite in Java
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
在Linux-Ubuntu上配置开发JavaFx程序-(更新)
前言 不清楚什么原因,网络上关于JavaFx的中文资料很少,并且都很老,所以建议大家有兴趣学习JavaFx还是看官方文档比较好,而且貌似部分人不看好JavaFx。 关于Swing的资料倒是要多很多。我觉得挺奇怪的,从设计上来将,还是JavaFx高明一些嘛。 PS:经过近一周的折腾和探索,发现OpenJDK使用JavaFX真的好多坑~ OpenJDK 和 Oracle JDK的配置是有差别的,这里推
2022-05-27 · Java · 约 1 分钟
2020年第一次用上Struts2框架做Web开发的体验
SliiyStruts2 又名 sb-struts2,因为struts真的太难用了(也许在很多年前是很好用的,但是现在看来,被其他框架秒成渣) 前言 想不到我还是得和这struts框架打交道啊,从一开始学web的时候就十分抵制这类古老,使用反人类的框架,不过为了帮女朋友做个学校的作业,还是得搞一下,然而,半小时就写好的业务代码,因为我不熟悉这个框架和Java的这套体系,调试了半天才成功run起来…
2022-06-17 · Java · 约 1 分钟
JavaWeb开发---Servlet的生命周期实验
前言 Servlet是运行在服务器端的程序,它的运行状态由Servlet容器(简称容器)来维护。 通常,在容器收到客户对Servlet的请求时,容器会判断这个Servlet是否为第一次被访问,如果是第一次被访问,则会创建一个Servlet实例同时调用该实例的init()方法,进行初始化。 每个Servlet只会被创建一个实例,同时也只会被初始化一次。然后将这个实例一直保存在内存中,对所有的请求进行
2022-05-27 · Java · 约 1 分钟
Java开发:JDK安装和环境配置
前言 环境配置是开发中最基础的部分,不过最近有很多新入坑的同学,都在问怎么安装JDK和配置环境,索性写一篇。 这里要注意一下,在Windows平台和Linux平台下做开发是不同的,Windows平台开发Java需要自己手动配置JDK的环境变量,而大多数Linux都是不需要的,安装了OpenJDK就可以了(可能很多发行版都自带了OpenJDK),这点是比较方便的。 下载Java JDK Window
2023-02-08 · Java · 约 1 分钟
讨论
共 0 条加载评论中…