简体   繁体   中英

Why is the file Read-Only?

I have got a Microsoft Access database in the resource folder of my Java application. When the user clicks a button, this database is copied to the temp directory of the PC. Then I make a temporary VBS file in the same directory and execute it. (This VBS file calls a VBA macro within the database, that deletes some records.) However, as the macro attempts to delete the records an error is thrown stating that the database is read only. Why does this happen?

Here is my code:

When the user clicks the button, some variables are set and then the following code is executed:

private void moveAccess() throws IOException {
    String dbName = "sys_cl_imp.accdb";
    String tempDbPath = System.getenv("TEMP").replace('\\', '/') + "/" + dbName;
    InputStream in = ConscriptioLegere.class.getResourceAsStream("res/" + dbName);
    File f = new File(tempDbPath);
    Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING);

    this.dbFilePath = tempDbPath;
    System.out.println("access in temp");
    f =  null;
}

Then a connection is made to the database to update some data; with

Connection con = DriverManager.getConnection("jdbc:ucanaccess://" + dbFilePath);            
Statement sql = con.createStatement();
...
sql.close();
con.close();

Afterwards this is executed:

public boolean startImport() {
    File vbsFile = new File(vbsFilePath);
    PrintWriter pw;
    try {
        updateAccess();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    try{
        pw = new PrintWriter(vbsFile);
        pw.println("Set accessApp = CreateObject(\"Access.Application\")");
        pw.println("accessApp.OpenCurrentDatabase (\"" + dbFilePath + "\")");
        pw.println("accessApp.Run \"sys_cl_imp.importData\", \"" + saveLoc + "\"");
        pw.println("accessApp.CloseCurrentDatabase");
        pw.close();

        Process p = Runtime.getRuntime().exec("cscript /nologo \"" + vbsFilePath + "\"");

While the process is running, the error occurres. I don't understand why the database is open as ReadOnly.

I tried setting f to null after the copying of the db, but it proved not to work that way.

Based on this dicussion .
The solution is adding ;singleconnection=true to JDBC url. UCanAccess will close the file after JDBC connection closed.

Connection con = DriverManager.getConnection("jdbc:ucanaccess://" + dbFilePath +";singleconnection=true");

Thank you for your solution beckyang. I managed to get it working with it, but there was a second mistake: I deleted the contents of a table with java then closed the connection and run the vba procedure. In the VBA I was attempting to delete the data again; but as there were none, this didn't work out. After deleting the SQL from the VBA, the project worked :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM