• 【文件搜索项目】使用jdbc操作SQLite


    一. 插入(采用变量的方式插入)

    1.创建数据源.DateSource
    2.建立连接
    3.构造SQL语句
    4.执行SQL语句
    5.释放资源

    public class TestSQLite {
        public static void main(String[] args) throws SQLException {
            textInsert();
        }
    
        public static void textInsert() throws SQLException {//插入操作
            //1.创建数据源.DataSource
            DataSource dataSource = new SQLiteDataSource();
            ((SQLiteDataSource) dataSource).setUrl("jdbc:sqlite://D:/App/sqlite-tools-win-x64-3450100/text.db");
            //2.建立连接
            Connection connection = dataSource.getConnection();
            //3.构造SQL语句
            String sql = "insert into text values(?,?)";
            PreparedStatement Statement = connection.prepareStatement(sql);
            Statement.setInt(1,11);
            Statement.setString(2,"张三");
            //4.执行SQL语句
            Statement.executeUpdate();
            //5.释放资源 根据反顺序释放资源
            Statement.close();
            connection.close();
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    写好代码后,我们需要点击运行,代码左边的绿色三角,运行结束后,去sqlite3.exe所在的文件夹中,shift+右键,进入shell控制台,查询增加结果

    PS D:\App\sqlite-tools-win-x64-3450100> .\sqlite3.exe text.db
    SQLite version 3.45.1 2024-01-30 16:01:20 (UTF-16 console I/O)
    Enter ".help" for usage hints.
    sqlite> select * from text;
    1|黄橙子
    2|沈月本人
    3|驼女士
    11|张三
    sqlite>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二.查询

    sqlite中进行查询
    1.创建数据源.DateSource
    2.建立连接
    3.构造SQL语句
    4.执行SQL语句
    5.遍历结果集合
    6.释放资源

    public static void textSelect() throws SQLException {//查询操作
            //从sqlite中进行查询
    
            //1.创建数据源.DateSource
            DataSource dataSource = new SQLiteDataSource();
            ((SQLiteDataSource)dataSource).setUrl("jdbc:sqlite://D:/App/sqlite-tools-win-x64-3450100/text.db");
            //描述数据源 前缀一定要写好jdbc:sqlite://,后面再跟上该数据库所在的地址
    
            //2.建立连接
            Connection connection = dataSource.getConnection();
    
            //3.构造SQL语句
            String sql = "select * from text";
            PreparedStatement statement = connection.prepareStatement(sql);
    
            //4.执行SQL语句
            ResultSet resultSet = statement.executeQuery();
    
            //5.遍历结果集合
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                System.out.println("id:"+id+",name:"+name);
            }
    
            //6.释放资源
            resultSet.close();
            statement.close();
            connection.close();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    运行结果在这里插入图片描述

  • 相关阅读:
    上海亚商投顾: 市场再现弱势调整 节日气氛浓厚
    vue安装和卸载依赖包package.json
    【数据结构】分块查找
    社保和五险一金那些事
    十三、MySQL(DQL)语句执行顺序
    强化学习环境 - robogym - 学习 - 1
    Docker 镜像详细操作
    Kubernetes上安装Metrics-Server
    智能导诊(Intelligent Guidance,IG)源码
    cdh集群搭建(6.3)
  • 原文地址:https://blog.csdn.net/weixin_52411749/article/details/136263120