【问题标题】:Why can't I insert or update a SQLite database from an executable JAR file?为什么我不能从可执行 JAR 文件插入或更新 SQLite 数据库?
【发布时间】:2011-03-29 22:02:55
【问题描述】:

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

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

有什么我必须做的解决方法吗?

该驱动程序在我的 Eclipse 环境中测试时运行良好,但在 JAR 文件中似乎无法独立运行。任何帮助将不胜感激。

下面是我正在做的代码sn-p:

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();

        }

    }
}

【问题讨论】:

  • 你在创建sqlite数据库的文件夹中有写权限吗?
  • 是的,它在我的虚拟机桌面上。没有对文件设置权限。
  • 什么意思它不起作用?你有例外吗?简单的事情不会发生吗? (使用e.printStackTrace() 而不是e.getMessage(),这通常会提供更多详细信息。)
  • 我认为没有显示任何错误消息,只是数据没有保存到数据库中。
  • 好的。 “提取数据库”显然不是问题。你是如何运行 Jar 的?它是否有一个指定主类的清单,以便您可以双击它来启动它?它是使用 JWS 启动的吗?作为小程序?从命令行或 .bat 文件或等效文件? (我试图找到的是 - 是否有安全经理到位?)

标签: java sqlite insert executable-jar


【解决方案1】:

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

没有。无法更新应用程序运行时类路径上的 Jars 中的条目。罐子通常是锁定的。

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

【讨论】:

  • 数据库位于可执行 jar 之外。我可以从数据库文件中读取,但不能写入它。似乎创建了一个日志文件(可能有一个待处理的事务在里面?)。
猜你喜欢
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多