【Java】【sqlite】【数据库】

一、maven依赖

        <!--        操作sqlite数据库 start-->
        <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.7.2</version>
        </dependency>
        <!--        操作sqlite数据库 end-->
        
        <!--        lombok插件 start-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <!--        lombok插件 end-->

 

二、封装工具类

备注:可直接copy使用

package com.database;

import lombok.Data;

import java.sql.*;

/**
 * @Author: Jarvis
 * @Date: 2020/6/28 23:44
 * @Version: v1.0.0
 * @Description:
 */
@Data
public class ZgxSqliteDbUtil {
    public enum SqlType {
        UPDATE("更新"),
        QUERY("查询");

        String desc = "";

        SqlType(String desc) {
            this.desc = desc;
        }

        public String getDesc() {
            return desc;
        }
    }

    private Connection conn;
    private Statement stat;

    public ZgxSqliteDbUtil(String dbPath) {
        //连接SQLite的JDBC
        try {
            Class.forName("org.sqlite.JDBC");

            //建立一个数据库名dbPath路径下的db的连接,如果不存在就在当前目录下创建
            Connection conn = DriverManager.getConnection(String.format("jdbc:sqlite:%s", dbPath));
            Statement stat = conn.createStatement();
            this.stat = stat;
            this.conn = conn;

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void closeConnect() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能:执行sql语句
     *
     * @param sqlType sql语句类型(更新SqlType.UPDATE、查询SqlType.QUERY)
     * @param sql     sql语句
     */
    public void execSql(SqlType sqlType, String sql) {
        try {
            // 场景1 更新操作
            if (sqlType == SqlType.UPDATE) {
                stat.executeUpdate(sql);
                System.out.println(String.format("执行SQL语句:%s", sql));
            }

            // 场景2 查询操作
            if (sqlType == SqlType.QUERY) {
                ResultSet resultSet = stat.executeQuery(sql);
                System.out.println(String.format("执行SQL语句:%s", sql));

                ResultSet rs = stat.executeQuery("select * from tbl1;"); //查询数据
//                while (rs.next()) { //将查询到的数据打印出来
//                    System.out.print("name = " + rs.getString("name") + " "); //列属性一
//                    System.out.println("salary = " + rs.getString("salary")); //列属性二
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2020-06-29 01:27  淡怀  阅读(448)  评论(0编辑  收藏  举报