0

i am creating a .mdb file using java as i am working with MS-ACCESS database files. But i am facing one problem. Suppose a created a file named file.mdb using new File("file.mdb").createNewFile();. No doubt it creates the file successfully, but when i try to open it, it says Unrecognized file. Can you help me?

If you dont know this solution then give me knowledge about any other database which creates database file (e.g. oracle doesnt create database files) so that i can create that file through java and access it.

  • Why would you expect a file you create using, presumably, the java.io.File class to be in the format of an Access database file? You would need some sort of library that knows how to read and write Access data files directly or you'd need to make a JDBC connection to the Access database. The latter would be more standard but the former appears to be possible as well. – Justin Cave Aug 02 '15 at 06:21
  • Please [edit] your question to include a tag for the software you are using to manipulate the Access database from Java: UCanAccess, Jackcess, or JDBC-ODBC. – Gord Thompson Aug 02 '15 at 08:06
  • @JustinCave then help me out. – Sandeep Dass Aug 02 '15 at 09:38

1 Answers1

0

As you have discovered,new File("file.mdb").createNewFile(); creates a zero-length file, and that is not a valid Access database file. Even an "empty" Access database contains system tables and other required information.

If you were using the UCanAccess JDBC driver then you could simply do the following to automatically create the database if it doesn't already exist:

String connStr = "jdbc:ucanaccess://C:/path/to/file.mdb;newdatabaseversion=V2003";
try (Connection conn = DriverManager.getConnection(connStr)) {
    // ... do stuff here
}

For more information on how to set up UCanAccess see

Manipulating an Access database from Java without ODBC

On the other hand, if you absolutely need to stick with the JDBC-ODBC Bridge (which is obsolete and has been removed from Java 8) then you'll probably need to use ADOX code similar to the second example in my related answer here.

Community
  • 1
  • 1
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
  • How can i use UCanAccess? because i prefer using Notepad++ instead of netbeans or any other development application. @GordThompson – Sandeep Dass Aug 02 '15 at 16:53
  • @SandeepDass - You can modify your CLASSPATH environment variable to include the full path to all five JAR files. – Gord Thompson Aug 02 '15 at 17:31