带有 JFileChooser 的 UCanAccess:"Empty database file"

UCanAccess with JFileChooser: "Empty database file"

我设置了我的代码,以便用户可以输入 Microsoft Access 文件的名称,然后文件就成功创建了。但是,我想使用 JFileChooser 使过程更顺畅,但是我的代码在使用它时不起作用。

这是我以前的(工作)代码,删除了一些不相关的代码-

try {

    fileName = JOptionPane.showInputDialog(null,"Enter file name ");
   String dbPath = "C:/Users/Evan/Documents/"+fileName+".accdb";

       System.out.println(dbPath);
       // outputs C:/Users/Evan/Documents/fileName.accdb
try (Connection conn = DriverManager.getConnection(

    "jdbc:ucanaccess://" + dbPath  
 +   ";newdatabaseversion=V2010"
)) {
 DatabaseMetaData dmd = conn.getMetaData();
    try (ResultSet rs = dmd.getTables(null, null, "Database", new String[] { "TABLE" })) {

        try (Statement s = conn.createStatement()) {
            s.executeUpdate("CREATE TABLE " + "Database " 
+" (Row COUNTER PRIMARY KEY, A DOUBLE , B DOUBLE)");
            System.out.println("File " + fileName + " created.");
            valueProperty.setValue(fileName);
        }

}
conn.close();

 }

}
 catch (Exception f){
  f.printStackTrace();
 }
       }

这是我现在的代码,输出错误

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.2 Empty database file

并生成一个文件,该文件不包含错误消息 "unrecognized database format" 以及文件的名称和路径,尽管该文件的路径与除文件名外的其他工作文件相同:

try {

    JFileChooser chooser = new JFileChooser();
   chooser.setCurrentDirectory(new File("/home/me/Documents"));
int retrieval = chooser.showSaveDialog(null);
if (retrieval == JFileChooser.APPROVE_OPTION) {
   FileWriter fw = new FileWriter(chooser.getSelectedFile()+".accdb");


  path=chooser.getSelectedFile().getAbsolutePath()+".accdb";
 // System.out.println(path);
fileName=chooser.getSelectedFile().getName();

   String dbPath = path.replace("\","/"); /* I know this looks weird, I
just did it because the output in the working version has it with / instead. 
I've tried it with both slashes and the result is the same. */   

   System.out.println(dbPath);
   // outputs C:/Users/Evan/Documents/fileName.accdb

/*where the code has an error */  try (Connection conn = DriverManager.getConnection(

    "jdbc:ucanaccess://" + dbPath  
 +   ";newdatabaseversion=V2010"
)) {
DatabaseMetaData dmd = conn.getMetaData();
    try (ResultSet rs = dmd.getTables(null, null, "Database", new String[] { 
"TABLE" })) {

        try (Statement s = conn.createStatement()) {
            s.executeUpdate("CREATE TABLE " + "Database" +" (Row COUNTER 
PRIMARY KEY, A DOUBLE , B DOUBLE)");
            System.out.println("File " + fileName + " created.");
            valueProperty.setValue(fileName);
        }

}
conn.close();

 }}
else 
    System.out.println("failed");
    return;

}
catch (Exception f){
  f.printStackTrace();
}

如果有人能提供任何帮助,我将不胜感激,谢谢

;newdatabaseversion=V2010 将创建一个新文件,如果它根本不存在。如果它存在,但包含零字节,则它不是有效的 Access 数据库。跟踪您的代码以查看它是否正在创建一个零字节文件,如果是,则在尝试打开您的连接之前删除该文件。