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);
            }
        }
    }
}
相关推荐
2501_937860944 小时前
Java 文件操作与IO(下):字节流、字符流实战IO读写
java·开发语言·python
xixingzhe24 小时前
spring boot项目接口访问慢问题解决方案
java·数据库·spring boot
MetaLite4 小时前
SpringBoot整合FastJson2数据脱敏-接口日志与失败降级
java·spring boot·后端
事圆则缓4 小时前
Kotlin 高阶工程化与 Android 深入实践
android·开发语言·kotlin
驭渊的小故事4 小时前
java抽奖项目-奖品上传中传递参数类型的错误
java·开发语言
名字还没想好☜4 小时前
Go 时间格式化为什么用 2006-01-02:time.Format/Parse 的参考时间、时区与解析踩坑
开发语言·后端·golang·go
段一凡-华北理工大学4 小时前
高炉智能布料技术与炉料分布优化~系列文章03:布料矩阵解码:环位、角度、圈数与料流的量化控制
java·网络·人工智能·矩阵·高炉智能化·高炉布料矩阵优化·高炉智能布料技术
Dream Cosmos4 小时前
C++ 多态下篇:虚函数表、动态绑定与多态底层原理
开发语言·c++
qfljg7 小时前
OpenRewrite 实战指南
java
Sylvia33.7 小时前
LOL实时数据接入深度解析:从WebSocket到完整数据模型
java·网络·python·websocket·网络协议