1

我已经提到了这个问题的选定答案:Java: Create MSAccess Database File (.mdb 0r .accdb) using Java

我的机器中有 MS Office 2010。我正在尝试创建访问数据库文件 (*.mdb / *.accdb)。但是,仍然没有创建文件本身,引发以下异常:

Exception in thread "main" java.io.FileNotFoundException: given file does not exist: C:\Users\473886\Desktop\employeedb1.mdb
    at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:360)
    at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:170)
    at mdb.MDBWriter.createDatabase(MDBWriter.java:93)
    at mdb.MDBWriter.startDatabaseProcess(MDBWriter.java:107)
    at mdb.MDBWriter.main(MDBWriter.java:120)

我使用了答案中可用的相同代码,并进行了一项修改,我使用了文件对话框,该对话框将询问我要将数据库文件保存在哪里:

public class MDBWriter {

    public static String saveFile(Frame f, String title, String defDir, String fileType) {
        FileDialog fd = new FileDialog(f, title, FileDialog.SAVE);
        fd.setFile(fileType);
        fd.setDirectory(defDir);
        fd.setLocation(50, 50);
        fd.show();
        return (fd.getDirectory() + "\\" + fd.getFile());
    }

    private static Database createDatabase(String databaseName) throws IOException {
//        return Database.create(new File(databaseName));
        File file = new File(databaseName);
        return new DatabaseBuilder(file)
        .setFileFormat(Database.FileFormat.V2010)
        .open();
    }

    private static TableBuilder createTable(String tableName) {
        return new TableBuilder(tableName);
    }

    public static void addColumn(Database database, TableBuilder tableName, String columnName, Types sqlType) throws SQLException, IOException {
        tableName.addColumn(new ColumnBuilder(columnName).setSQLType(Types.INTEGER).toColumn()).toTable(database);
    }

    public static void startDatabaseProcess() throws IOException, SQLException {
        String fileName = saveFile(new Frame(), "Save...", ".\\",   "*.mdb");
        String databaseName = "D:\\employeedb1.accdb"; // Creating an MS Access database
        Database database = createDatabase(fileName);

        String tableName = "Employee"; // Creating table
        Table table = createTable(tableName)
                .addColumn(new ColumnBuilder("Emp_Id").setSQLType(Types.INTEGER).toColumn())
                .addColumn(new ColumnBuilder("Emp_Name").setSQLType(Types.VARCHAR).toColumn())
                .addColumn(new ColumnBuilder("Emp_Employer").setSQLType(Types.VARCHAR).toColumn())
                .toTable(database);

        table.addRow(122875, "Sarath Kumar Sivan","Infosys Limited.");//Inserting values into the table
    }

    public static void main(String[] args) throws IOException, SQLException {
        startDatabaseProcess();
    }

}

请提出一些解决方案。

4

3 回答 3

1

如果要创建数据库,则需要调用DatabaseBuilder.create(),而不是open()(打开现有数据库)。

于 2013-10-10T19:15:44.437 回答
1

无法从 [Java] 代码创建 [一个 Access 数据库] !

废话。以下适用于任何带有 Java 和Jackcess的平台...

import com.healthmarketscience.jackcess.*;
import java.io.File;

public class bar {

    public static void main(String[] args) {
        try {
            DatabaseBuilder.create(Database.FileFormat.V2010, new File("/home/gord/jTest.accdb"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Done!");
    }

}

..以下代码在没有 Jackcess 的情况下在 Windows 上的 Java 中工作(但需要 Access 数据库引擎)...

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CreateAccdb {

    public static void main(String[] args) {
        String databaseName = "C:\\__tmp\\employeedb1.accdb";

        String tempScriptName = System.getenv("TEMP") + "\\$$CreateAccdbScript.vbs";
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(tempScriptName));
            out.write("Set cat = CreateObject(\"ADOX.Catalog\")");
            out.newLine();
            out.write("cat.Create \"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + databaseName + ";\"");
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String cmd = "cscript " + tempScriptName;
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            p.waitFor();
            BufferedReader rdr = 
                    new BufferedReader(new InputStreamReader(p.getErrorStream()));
            int errorLines = 0;
            String line = rdr.readLine();
            while (line != null) {
                errorLines++;
                System.out.println(line);  // display error line(s), if any
                line = rdr.readLine();
            }
            if (errorLines == 0) {
                System.out.println("The operation completed successfully.");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

        try {
            Files.deleteIfExists(Paths.get(tempScriptName));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
于 2013-10-09T09:12:17.743 回答
0

根据此处提供的答案。唯一的解决方案是复制现有的空 mdb,连接到此并创建表等。无法从代码创建 mdb!

于 2013-10-09T05:56:49.537 回答