У меня есть приложение, которое я развертываю как исполняемый файл 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();
}
}
}