开发者

Why can't I insert or update a SQLite database from an executable JAR file?

开发者 https://www.devze.com 2023-02-20 10:47 出处:网络
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,

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(){开发者_C百科

        //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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消