SQLite Data Base 实现

   /**
     * jdbc的key值
     */
    private static String jdbcKey = "jdbc:sqlite:";

    /**
     * SQLITE_JDBC_DRIVER 驱动
     */
    private static final String SQLITE_JDBC_DRIVER = "org.sqlite.JDBC";

    /**
     * 数据库的连接
     */
    private Connection conn = null;

    /**
     * 数据库的路径
     */
    private String path = "";

    /**
     * 连接数据库
     * @param dbUrl 数据库链接
     */
    public SqliteDataBase(String dbUrl)
    {
        this.conn = null;
        this.path = dbUrl;
    }

   /**
     * 关闭连接
     */
    @Override
    public void close()
    {
        try
        {
            if (null != conn && !this.conn.isClosed())
            {
                // 关闭连接
                this.conn.close();
                this.conn = null;
            }
        }
        catch (SQLException e)
        {
            LogManager.recordLog(LogType.error, e.getMessage(), e);
        }
    }

    /**
     * 执行SQL语句
     * @param sql sql语句
     * @return boolean
     */
    @Override
    public boolean execute(String sql)
    {
        return execute(sql, true);
    }

    /**
    * 执行SQL语句
    * @param sql sql语句
    * @param writeNow 是否写入
    * @return boolean
    */
    @Override
    public boolean execute(String sql, boolean writeNow)
    {
        // 如果没有打开,直接返回false
        if (!this.isOpened())
        {
            return false;
        }

        // 返回值
        boolean result = true;

        // 执行sql语句
        Statement statement = null;

        try
        {
            // 创建statement
            statement = this.conn.createStatement();

            // 执行sql语句
            statement.execute(sql);

            // this.conn.commit();
        }
        catch (SQLException e)
        {
            LogManager.recordLog(LogType.warn, "SqliteDatabase.execute", e);
            result = false;
        }

        // 必须执行
        finally
        {

            try
            {
                // 最后均直接提交commit
                this.conn.commit();
            }
            catch (SQLException e)
            {
                LogManager.recordLog(LogType.error, e.getMessage(), e);
            }
            try
            {
                // 关闭statement
                if (null != statement)
                {
                    statement.close();
                    statement = null;
                }
            }
            catch (SQLException e)
            {
                LogManager.recordLog(LogType.error, e.getMessage(), e);
                result = false;
            }
        }

        return result;
    }

    /**
     *
     * 提交
     * @return boolean
     */
    @Override
    public boolean flush()
    {
        try
        {
            this.conn.commit();
            return true;
        }
        catch (SQLException e)
        {
            LogManager.recordLog(LogType.error, e.getMessage(), e);
            return false;
        }
    }

    /**
     * 打开数据库
     * @return boolean
     */
    @Override
    public boolean open()
    {
        // 如果已经打开数据库,直接返回true
        try
        {
            if (null != this.conn && !this.conn.isClosed())
            {
                return true;
            }
            String url = SqliteDataBase.jdbcKey + this.path;

            File dbDir = new File(path.substring(0, path.lastIndexOf("\")));
            if (!dbDir.exists())
            {
                try
                {
                    dbDir.mkdir();
                }
                catch (SecurityException e)
                {
                    LogManager.recordLog(LogType.error, e.getMessage(), e);
                }
            }

            Class.forName(SQLITE_JDBC_DRIVER);

            // 获取连接
            this.conn = DriverManager.getConnection(url);

            // 设置自动提交
            this.conn.setAutoCommit(false);
        }
        catch (SQLException e)
        {
            LogManager.recordLog(LogType.error, e.getMessage(), e);
            return false;
        }
        catch (Exception e)
        {
            LogManager.recordLog(LogType.error, e.getMessage(), e);
            return false;
        }

        // 重新打开数据库
        return true;
    }

原文地址:https://www.cnblogs.com/skay--001/p/3723467.html