Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';我是否从可执行JAR文件插入或更新SQLite数据库?_Java_Sqlite_Insert_Executable Jar - Fatal编程技术网

Java 为什么可以';我是否从可执行JAR文件插入或更新SQLite数据库?

Java 为什么可以';我是否从可执行JAR文件插入或更新SQLite数据库?,java,sqlite,insert,executable-jar,Java,Sqlite,Insert,Executable Jar,我有一个作为可执行JAR文件部署的应用程序。最初,这个JAR文件将与MySQL数据库通信,但最近我决定改为使用SQLite。但是,在测试时,我发现在从JAR文件运行应用程序时,无法插入或更新数据库 我正在使用来自以下网站的JDBC驱动程序: 我有什么变通办法吗 该驱动程序在我的Eclipse环境中进行测试时运行良好,但在JAR文件中似乎无法独立运行。任何帮助都将不胜感激 下面是我正在做的工作的代码片段: public abstract class AbstractDataUpdator impl

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

我正在使用来自以下网站的JDBC驱动程序:

我有什么变通办法吗

该驱动程序在我的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();

        }

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

否。无法更新应用程序运行时类路径上JAR中的条目。罐子通常是锁着的


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

您在创建sqlite数据库的文件夹中是否具有写入权限?是的,它位于我的虚拟机桌面上。没有对该文件设置权限。什么意思是它不工作?你有例外吗?难道什么都没有发生吗?(使用
e.printStackTrace()
而不是
e.getMessage()
,这通常会提供更多详细信息。)我认为不会显示任何错误消息,只是数据没有保存到数据库中。确定。“提取数据库”显然不是问题所在。你是怎么操作这个罐子的?它是否有指定主类的清单,以便您可以双击它来启动它?它是使用JWS启动的吗?作为小程序?从命令行或.bat文件或等效文件?(我试图找到的是——是否有一个安全管理器在适当的位置?)数据库位于可执行jar之外。我可以读取数据库文件,但不能写入。似乎创建了一个日志文件(可能有一个挂起的事务在里面?)。