为什么我无法从可执行 JAR 文件插入或更新 SQLite 数据库?

发布于 2024-10-28 01:21:31 字数 3067 浏览 3 评论 0原文

我有一个作为可执行 JAR 文件部署的应用程序。最初,这个 JAR 文件将与 MySQL 数据库通信,但最近我决定改用 SQLite。但是,在测试时,我发现从 JAR 文件运行应用程序时无法插入或更新数据库。

我使用以下网站的 JDBC 驱动程序: http://zentus.com/sqlitejdbc/index.html

我必须采取解决方法吗?

该驱动程序在我的 Eclipse 环境中测试时工作得很好,但似乎不能在 JAR 文件中独立工作。任何帮助将不胜感激。

下面是我正在做的事情的代码片段:

public abstract class AbstractDataUpdator implements DataUpdator{

    protected String description;
    public String[] fieldsToSelect;
    protected String queryString;
    protected String updateString;
    protected String tableName;
    protected String whereStatement;
    protected String groupByStatement;
    protected String orderByStatement;
    protected ResultSet queryResultSet;
    protected Connection connection;
    protected PreparedStatement preparedStatement;
    protected Statement statement;
    protected Database db;
    protected String uri, username, password;
    protected int dbIndex = 0;
    protected static int numInstances = 0;
    protected String countQueryString;
    protected int maxLookupNo = 0;
    protected boolean preparedStatementAlreadyCreated = false;

    AbstractDataUpdator(String description ){
        this.description = description;
        //this.fieldsToSelect = fieldsToSelect;
        //this.tableName = tableName;
        //setupDatabase(databaseName, serverName);

        numInstances++;

    }

    public void setupDatabase(String databaseName, String serverName) {
        // MySQL
        //uri = "jdbc:mysql://"+serverName+"/"+databaseName;

        // SQLite
        uri = "jdbc:sqlite:myfirst_sqlite_db";

        // If there is already a database object in the pool with this information we want to use that object
        // instead of creating another one.
        dbIndex = DatabasePool.getInstance().getIndexOfDatabaseWithThisInfo(uri, username, password);
        if( dbIndex == -1 ){
            db = new Database( uri, username, password );

        }else{
            db = DatabasePool.getInstance().getDatabaseAt(dbIndex);
        }

        try {
            connection = db.getConnection().getConnection();
            connection.setAutoCommit(true);
            statement = connection.createStatement();

        } catch (SQLException e) {
            System.out.println("SQL error occured in setupDatabase() --> "+e.getMessage());
            e.printStackTrace();
        }
    }

    public String executeUpdate(){

        //System.out.println(updateString);

        try {

            if( updateString != null){
                statement.executeUpdate( updateString );
                return null;
            }
            return "update string is null";

        } catch (SQLException e) {
            System.out.println("SQL error occured in executeUpdate()"+e.getMessage());
            return e.getMessage();

        }catch (OutOfMemoryError e){
            System.out.println("out of memory due to execute update function!!!!");
            return e.getMessage();

        }

    }
}

I have an application that I deploy as an executable JAR file. Originally, this JAR file would communicate with a MySQL database but recently I have decided I want to go with SQLite instead. However, while testing I found that I can't insert into or update the database when running my application from the JAR file.

I'm using the JDBC driver from the following website: http://zentus.com/sqlitejdbc/index.html

Is there a workaround I have to do?

The driver works great while testing in my Eclipse environment, but doesn't seem to work standalone in a JAR file. Any help would be greatly appreciated.

Below is a code snippet of what I'm doing:

public abstract class AbstractDataUpdator implements DataUpdator{

    protected String description;
    public String[] fieldsToSelect;
    protected String queryString;
    protected String updateString;
    protected String tableName;
    protected String whereStatement;
    protected String groupByStatement;
    protected String orderByStatement;
    protected ResultSet queryResultSet;
    protected Connection connection;
    protected PreparedStatement preparedStatement;
    protected Statement statement;
    protected Database db;
    protected String uri, username, password;
    protected int dbIndex = 0;
    protected static int numInstances = 0;
    protected String countQueryString;
    protected int maxLookupNo = 0;
    protected boolean preparedStatementAlreadyCreated = false;

    AbstractDataUpdator(String description ){
        this.description = description;
        //this.fieldsToSelect = fieldsToSelect;
        //this.tableName = tableName;
        //setupDatabase(databaseName, serverName);

        numInstances++;

    }

    public void setupDatabase(String databaseName, String serverName) {
        // MySQL
        //uri = "jdbc:mysql://"+serverName+"/"+databaseName;

        // SQLite
        uri = "jdbc:sqlite:myfirst_sqlite_db";

        // If there is already a database object in the pool with this information we want to use that object
        // instead of creating another one.
        dbIndex = DatabasePool.getInstance().getIndexOfDatabaseWithThisInfo(uri, username, password);
        if( dbIndex == -1 ){
            db = new Database( uri, username, password );

        }else{
            db = DatabasePool.getInstance().getDatabaseAt(dbIndex);
        }

        try {
            connection = db.getConnection().getConnection();
            connection.setAutoCommit(true);
            statement = connection.createStatement();

        } catch (SQLException e) {
            System.out.println("SQL error occured in setupDatabase() --> "+e.getMessage());
            e.printStackTrace();
        }
    }

    public String executeUpdate(){

        //System.out.println(updateString);

        try {

            if( updateString != null){
                statement.executeUpdate( updateString );
                return null;
            }
            return "update string is null";

        } catch (SQLException e) {
            System.out.println("SQL error occured in executeUpdate()"+e.getMessage());
            return e.getMessage();

        }catch (OutOfMemoryError e){
            System.out.println("out of memory due to execute update function!!!!");
            return e.getMessage();

        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2024-11-04 01:21:31

但是,在测试时,我发现从 JAR 文件运行应用程序时无法插入或更新数据库。

不可以。应用程序的运行时类路径上的 Jars 内的条目无法更新。罐子通常是锁着的。

因此,只读数据库可以部署在 Jar 中。为了更新,数据库首先需要扩展到文件系统。

However, while testing I found that I can't insert into or update the database when running my application from the JAR file.

No. Entries within Jars on the application's run-time class-path cannot be updated. The Jars are generally locked.

As a result of that, a read only DB can be deployed in a Jar. For update, the DB would first need to be expanded out to the file-system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文