JDBC操作SQLite的工具类

直接调用无需拼装sql

注入依赖

    
        org.xerial
        sqlite-jdbc
        3.43.0.0
    

工具类


import org.sqlite.SQLiteConnection;

/**
 * @Author cpf
 * @Date 2023/9/8
 */
import java.sql.*;

public class SQLiteUtils {
   
    private static final String DB_FILE = "src/main/resources/database.db"; // SQLite数据库文件名


    public static void main(String[] args) throws SQLException {
   
        Connection conn = getConnection();

        //创建表
        String[] columns = new String[]{
   "id", "name", "age", "gender"};
        createTable(conn, "students", columns);

        //插入
        Object[] column = new Object[]{
   "name", "age", "gender"};
        Object[] values = new Object[]{
   "张三", "16", "汉族"};
        insertRecord(conn, "students",column, values);

        //查询所有记录
        ResultSet students = queryAll(conn, "students");
        while (students.next()){
   
            System.out.println(students.getString("name") + " | " + students.getString("age") + " | " + students.getString("gender"));
        }

        //查询指定字段的记录
        ResultSet resultSet = queryByColumn(conn, "students", "name");
        while (resultSet.next()){
   
            System.out.println("姓名: " + resultSet.getString("name"));
        }

        //查询指定条件的记录
        String[] column01 = new String[]{
   "name"};
        Object[] values01 = new Object[]{
   "张三"};
        ResultSet students1 = queryByCondition(conn, "students", column01, values01);
        while (students1.next()){
   
            System.out.println(students1.getString("name") + " | " + students1.getString("age") + " | " + students1.getString("gender"));
        }

        /**
         * 更新一条记录
         * @param conn 数据库连接
         * @param tableName 表名
         * @param conditionSet 更新字段的数组
         * @param conditionSetValue 更新字段的数组
         * @param conditions 条件的数组
         * @param conditionsValue 条件值的数组
         */
        String[] conditionSet = new String[]{
   "age"};
        Object[] conditionSetValue = new Object[]{
   "45"};

        String[] conditions = new String[]{
   "id"};
        Object[] conditionsValue = new Object[]{
   "5"}

你可能感兴趣的:(JavaFx,sqlite,jdbc,javafx)