com.litongjava.lite.Lite Maven / Gradle / Ivy
package com.litongjava.lite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.litongjava.tio.utils.snowflake.SnowflakeIdUtils;
public class Lite {
public static final String sql_statement_stat = "sql_statement_stat";
public static final String DB_URL = "jdbc:sqlite:default_database.db";
public static final String driverClass = "org.sqlite.JDBC";
// SQL to create table if it doesn't exist
private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS " + sql_statement_stat + " (" +
//
"id LONG PRIMARY KEY, " +
//
"name TEXT, " +
//
"sqlType TEXT, " +
//
"sql TEXT, " +
//
"paras TEXT, " +
//
"rows INTEGER, " +
//
"startTimeMillis LONG, " +
//
"elapsed LONG)";
static {
// Load the SQLite JDBC driver
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try (Connection conn = DriverManager.getConnection(DB_URL)) {
if (!isTableExists(conn, sql_statement_stat)) {
try (Statement stmt = conn.createStatement()) {
stmt.execute(CREATE_TABLE_SQL);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// Method to check if table exists
private static boolean isTableExists(Connection conn, String tableName) throws SQLException {
String checkTableExistsSQL = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
try (PreparedStatement pstmt = conn.prepareStatement(checkTableExistsSQL)) {
pstmt.setString(1, tableName);
try (ResultSet rs = pstmt.executeQuery()) {
return rs.next();
}
}
}
public static void saveSqlStatementStat(String name, String sqlType, String sql, Object[] paras, int result, long startTimeMillis, long elapsed) {
String insertSQL = "INSERT INTO " + sql_statement_stat + " (id, name, sqlType, sql, paras, rows, startTimeMillis, elapsed) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
try (Connection conn = DriverManager.getConnection(DB_URL); PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
long id = SnowflakeIdUtils.id();
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.setString(3, sqlType);
pstmt.setString(4, sql);
pstmt.setString(5, paras != null ? Arrays.toString(paras) : "");
pstmt.setInt(6, result);
pstmt.setLong(7, startTimeMillis);
pstmt.setLong(8, elapsed);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy