为什么我无法从可执行 JAR 文件插入或更新 SQLite 数据库?
我有一个作为可执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可以。应用程序的运行时类路径上的 Jars 内的条目无法更新。罐子通常是锁着的。
因此,只读数据库可以部署在 Jar 中。为了更新,数据库首先需要扩展到文件系统。
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.