文章來源地址http://www.zghlxwxcb.cn/news/detail-793264.html
引入 依賴
<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;
//驅(qū)動名稱
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;
}
/**
* 關(guān)閉連接
*
* @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();
}
}
}
}
//關(guān)閉連接和 執(zhí)行 的打開資源
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();
}
}
}
}
// 插入數(shù)據(jù)
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();
// 執(zhí)行批處理
pstmt.executeBatch();
}catch (SQLException e){
e.printStackTrace();
}
sqlite_conn.setAutoCommit(false); // 關(guān)閉自動提交事務(wù)
// 更新設(shè)置信息到數(shù)據(jù)庫
// 更新 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();
// 提交事務(wù)
sqlite_conn.commit();
文章來源:http://www.zghlxwxcb.cn/news/detail-793264.html
到了這里,關(guān)于運維知識點-Sqlite的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!