运维知识点-Sqlite

Sqlite

  • [引入 依赖](#引入 依赖)

引入 依赖

csharp 复制代码
            <dependency>
                <groupId>org.xerial</groupId>
                <artifactId>sqlite-jdbc</artifactId>
                <version>3.36.0.3</version>
            </dependency>
csharp 复制代码
 

import javafx.scene.control.Alert;
import java.sql.*;

public class DbUtil {

    private static String DB_PATH = "db/database.db";
    private static String sqliteURL="jdbc:sqlite:" + DB_PATH;
    //驱动名称
    private  static  String jdbcNameSqlite = "org.sqlite.JDBC";

    public static  Connection getSqliteCon() throws SQLException {
        try {
            Class.forName(jdbcNameSqlite);
            Connection conn = null;
            conn = DriverManager.getConnection(sqliteURL);
            return conn;
        } catch (Exception e){
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText(null);
//        alert.setContentText(ResultMsg.DB_SQLITE_ERROR.getMsg());
        alert.setContentText(String.valueOf(e));
        alert.showAndWait();
        e.printStackTrace();
    }
        return null;
    }

    /**
     * 关闭连接
     *
     * @throws Exception
     */
    public static void close(ResultSet rs, PreparedStatement st, Connection con) throws SQLException {
        if (rs != null) {
            rs.close();
            if (st != null) {
                st.close();
                if (con != null) {
                    con.close();
                }
            }
        }
    }

    //关闭连接和 执行 的打开资源
    public static void close(PreparedStatement st, Connection con) throws SQLException {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
csharp 复制代码
                    // 插入数据
                    String sqlInsert = "INSERT INTO setting (id,set_name,set_value) VALUES (?,?,?)";
                    PreparedStatement pstmt = sqlite_conn.prepareStatement(sqlInsert);


                    try{


                        pstmt.setInt(1,1);
                        pstmt.setString(2,"nuclei_temp");
                        pstmt.setString(3,"");
                        pstmt.addBatch();

                        pstmt.setInt(1,2);
                        pstmt.setString(2,"fingerpath");
                        pstmt.setString(3,"");
                        pstmt.addBatch();

                        pstmt.setInt(1,3);
                        pstmt.setString(2,"eholepath");
                        pstmt.setString(3,"");
                        pstmt.addBatch();

                        pstmt.setInt(1,4);
                        pstmt.setString(2,"nucleiexe");
                        pstmt.setString(3,"");
                        pstmt.addBatch();

//                        pstmt.executeUpdate();
                        // 执行批处理
                        pstmt.executeBatch();

                    }catch (SQLException e){
                        e.printStackTrace();
                    }
csharp 复制代码
                    sqlite_conn.setAutoCommit(false); // 关闭自动提交事务
                    // 更新设置信息到数据库
                    // 更新 nuclei_temp
                    String sqlUpdateNucleiTemp = "REPLACE INTO setting (id, set_name, set_value) VALUES (1, 'nuclei_temp', ?)";
                    PreparedStatement psUpdateNucleiTemp = sqlite_conn.prepareStatement(sqlUpdateNucleiTemp);
                    psUpdateNucleiTemp.setString(1, nuclei_temp);
                    psUpdateNucleiTemp.executeUpdate();

                    // 更新 fingerpath
                    String sqlUpdateFingerPath = "REPLACE INTO setting (id, set_name, set_value) VALUES (2, 'fingerpath', ?)";
                    PreparedStatement psUpdateFingerPath = sqlite_conn.prepareStatement(sqlUpdateFingerPath);
                    psUpdateFingerPath.setString(1, fingerpath);
                    psUpdateFingerPath.executeUpdate();

                    // 更新 eholepath
                    String sqlUpdateEholePath = "REPLACE INTO setting (id, set_name, set_value) VALUES (3, 'eholepath', ?)";
                    PreparedStatement psUpdateEholePath = sqlite_conn.prepareStatement(sqlUpdateEholePath);
                    psUpdateEholePath.setString(1, eholepath);
                    psUpdateEholePath.executeUpdate();

                    // 更新 nucleiexe
                    String sqlUpdateNucleiExe = "REPLACE INTO setting (id, set_name, set_value) VALUES (4, 'nucleiexe', ?)";
                    PreparedStatement psUpdateNucleiExe = sqlite_conn.prepareStatement(sqlUpdateNucleiExe);
                    psUpdateNucleiExe.setString(1, nucleiexe);
                    psUpdateNucleiExe.executeUpdate();

                    // 提交事务
                    sqlite_conn.commit();
相关推荐
cui_win30 分钟前
redis 内存使用率高居高不下,如何分析 key占用情况
数据库·redis·junit·rdb
学Java的bb2 小时前
后端Web实战-MySQL数据库
数据库·mysql
Elastic 中国社区官方博客2 小时前
超越相似名称:Elasticsearch semantic text 如何在简洁、高效、集成方面超越 OpenSearch semantic 字段
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Warren982 小时前
Java Record 类 — 简化不可变对象的写法
java·开发语言·jvm·分布式·算法·mybatis·dubbo
头发还在的女程序员4 小时前
ThinkPHP+Mysql 灵活用工小程序-技术深度解析与实践指南
数据库·mysql·小程序
꒰ঌ 安卓开发໒꒱4 小时前
SQL Server安全删除数据并释放空间的技术方案
数据库·安全·oracle
Co0kie_4 小时前
SpringAI报错:com.github.victools.jsonschema.generator.AnnotationHelper
jvm·spring boot·ai·ai编程
用户848508146905 小时前
SurrealDB 快速上手教程
数据库·后端
kura_tsuki5 小时前
[Oracle数据库] ORACLE的用户维护和权限操作
数据库·oracle
蚰蜒螟5 小时前
JVM安全点轮询汇编函数解析
汇编·jvm·安全