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);
            }
        }
    }
}
相关推荐
快乐肚皮34 分钟前
堆排序详解:从理论到实践
java·算法·排序算法·堆排序
hqxstudying38 分钟前
Kafka
java·spring cloud·kafka
悟能不能悟41 分钟前
Logback-spring.xml 配置屏蔽特定路径的日志
xml·java·spring
小伍_Five1 小时前
spark数据处理练习题番外篇【下】
java·大数据·spark·scala
qqxhb1 小时前
零基础设计模式——行为型模式 - 中介者模式
java·设计模式·go·中介者模式
forestsea1 小时前
Maven 多仓库治理与发布策略深度实践
java·maven
陌殇殇1 小时前
Hadoop 003 — JAVA操作MapReduce入门案例
java·hadoop·mapreduce
march of Time1 小时前
图数据库介绍及应用,go和Java使用图数据库
java·数据库·golang
委婉待续1 小时前
Qt的学习(三)
开发语言·qt·学习
CodeCraft Studio1 小时前
PDF处理控件Aspose.PDF教程: 在 Java 中编辑 PDF 页面大小
java·pdf·aspose·aspose.pdf