返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Java

Java使用JDBC操作SQLite

SQLite介紹 SQLite是一個輕量級的資料庫系統,不需要安裝就可以使用,也可以十分容易的就內嵌於系統內,FireFox就內嵌SQLite,可以在FireFox上直接使用JavaScript來呼叫操作資料庫。 SQLite是由C語言撰寫而已,可以跨Linux及Windows等平台,在Java存取及操作上則可使用JDBC來連線SQLite。 在JDBC連線SQLite上,大概分成二種方式,一種是

曦远 (DealiAxy)约 11 分钟
目录4 个章节

SQLite介紹

SQLite是一個輕量級的資料庫系統,不需要安裝就可以使用,也可以十分容易的就內嵌於系統內,FireFox就內嵌SQLite,可以在FireFox上直接使用JavaScript來呼叫操作資料庫。

SQLite是由C語言撰寫而已,可以跨Linux及Windows等平台,在Java存取及操作上則可使用JDBC來連線SQLite。

在JDBC連線SQLite上,大概分成二種方式,一種是由Pure-Java來連結資料,另一種則是直接利用Java呼叫C語言撰寫的函式庫,不過在實測上直接呼叫C的函式庫的方式應該是比較快的,不過在無法找到合適C函式庫的平台則可使用Pure-Java版。

SQLiteJDBC目前查到這個版本是比較有在更新,而且在使用上跟一般的JDBC幾乎是一樣的,上手程式十分簡單。

SQLiteJDBC可以由這裡下載,以下範例是由3.5.7版本製作,提供建立Table、移除Table、查詢、新增、刪除及修改等範例。

實例代碼

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();
        
    }
}

說明

SQLite並沒有使用者登入的的機制,所以只需要告知要存取的資料庫檔案位置就可以使用了。jdbc:sqlite:sample.db其中sample.db就是檔案名稱,也可指定其路徑位置c:\sample.db。

以下是執行結果:

原本沒有sample.db這個檔案也被自動建立了,在專案目錄底下。

參考資料

https://blog.yslifes.com/archives/971

_EOF_

Java使用JDBC操作SQLite

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

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 分钟

Spring-Boot:创建第一个应用

简介 Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作。在本教程中,将学习如何使用 Spring Boot 的 starter、特性和可执行 JAR 文件结构,快速创建能直接运行的基于 Spring 的应用程序。 本文使用IDEA作为开发工具,Gradle作为构建工具,创建一个简单的SpringBoot应用,暂时不涉及数据库的配置。 使用IDEA创建

2022-05-27 · Java · 约 1 分钟

深入Jar包:Gradle构建可执行jar包与访问jar包中文件夹与文件

前言 Java的跨平台功能听起来很诱人可口,号称“Write Once,Run Everywhere”,实际上是“Run Once,Debug Everywhere”... 在实际开发过程中还是会遇到各种各样的坑的,刚刚解决了一系列问题,特地写个文章总结一下。 使用Gradle构建Jar包 感谢万能的Gradle,极大提高了Java开发的生产力~ 在Gradle中生成jar包可以使用官方的插件:a

2022-05-27 · Java · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…