运维知识点-Sqlite

在这里插入图片描述

Sqlite

引入 依赖

            <dependency>
                <groupId>org.xerial</groupId>
                <artifactId>sqlite-jdbc</artifactId>
                <version>3.36.0.3</version>
            </dependency>



 

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();
            }
        }
    }
}



                    // 插入数据
                    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();
                    }

                    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();

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 08:08:01       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 08:08:01       97 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 08:08:01       78 阅读
  4. Python语言-面向对象

    2024-01-17 08:08:01       88 阅读

热门阅读

  1. @Transactional注解不生效的几种场景

    2024-01-17 08:08:01       62 阅读
  2. python爬虫01-爬虫介绍

    2024-01-17 08:08:01       61 阅读
  3. Python桌面程序开发指南

    2024-01-17 08:08:01       52 阅读
  4. ACM论文LaTeX模板解析(五)| TODO

    2024-01-17 08:08:01       63 阅读
  5. mongoose安装和使用(超详细)

    2024-01-17 08:08:01       39 阅读
  6. 网络命令ping和telnet

    2024-01-17 08:08:01       44 阅读
  7. Go语言和C++语言比较

    2024-01-17 08:08:01       60 阅读
  8. css垂直水平居中的几种实现方式

    2024-01-17 08:08:01       55 阅读
  9. 小程序中使用wx.previewImage实现图片预览与缩放

    2024-01-17 08:08:01       44 阅读
  10. 电商API接口的大数据分析与挖掘技巧

    2024-01-17 08:08:01       59 阅读
  11. 设计模式-适配器模式

    2024-01-17 08:08:01       53 阅读