java sqlite 操作

java sqlite 库

https://static.runoob.com/download/sqlite-jdbc-3.7.2.jar

java 复制代码
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample
{
    public static void main(String[] args)
    {
        Connection connection = null;
        try
        {
            // create a database connection
            Class.forName("org.sqlite.JDBC");  //重点
            connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(30);  // 设置超时为30秒

            statement.executeUpdate("drop table if exists person");  //创建数据库
            statement.executeUpdate("create table person (id integer, name string)");  //创建表
            statement.executeUpdate("insert into person values(1, 'leo')");  //添加
            statement.executeUpdate("insert into person values(2, 'yui')");  //添加
            ResultSet rs = statement.executeQuery("select * from person");
            while(rs.next())
            {
                // read the result set
                System.out.println("name = " + rs.getString("name"));
                System.out.println("id = " + rs.getInt("id"));
            }
        }
        catch(Exception e)
        {
            // if the error message is "out of memory",
            // it probably means no database file is found
            System.err.println(e.toString());
        }
        finally
        {
            try
            {
                if(connection != null)
                    connection.close();
            }
            catch(SQLException e)
            {
                // connection close failed.
                System.err.println(e);
            }
        }
    }
}
相关推荐
XH华27 分钟前
C语言第十一章内存在数据中的存储
c语言·开发语言
叫我阿柒啊2 小时前
Java全栈开发面试实战:从基础到微服务架构
java·vue.js·spring boot·redis·git·full stack·interview
小凡敲代码2 小时前
2025年金九银十Java面试场景题大全:高频考点+深度解析+实战方案
java·程序员·java面试·后端开发·求职面试·java场景题·金九银十
AndrewHZ2 小时前
【python与生活】如何用Python写一个简单的自动整理文件的脚本?
开发语言·python·生活·脚本·文件整理
拉法豆粉2 小时前
在压力测试中如何确定合适的并发用户数?
java·开发语言
枯萎穿心攻击3 小时前
Unity VS UE 性能工具与内存管理
开发语言·游戏·unity·ue5·游戏引擎·虚幻·虚幻引擎
爱上纯净的蓝天3 小时前
迁移面试题
java·网络·c++·pdf·c#
老赵的博客3 小时前
c++ 常用接口设计
开发语言·c++
binbinaijishu883 小时前
Python爬虫入门指南:从零开始的网络数据获取之旅
开发语言·爬虫·python·其他
chenglin0163 小时前
Logstash_Input插件
java·开发语言