gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:53:46 24 4
gpt4 key购买 nike

我有一个作为可执行 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();

}

}
}

最佳答案

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

没有。应用程序运行时类路径上的 Jars 内的条目无法更新。 jar 通常是锁着的。

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

关于java - 为什么我无法从可执行 JAR 文件插入或更新 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5479477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com