• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2500
  • Last Modified:

How to open Microsoft Access 2010 file from Java netbeans code

I have before been utilising the following code to get access to an Microsoft Acces database file from my java code in Netbeans, but now I connot get the connection to the database. What is wrong? Does Access 2010 require something special?

public class DBaccess {
//takes care of the connection to the simulation database
    private java.sql.Connection  con = null;
    private Statement stmt  = null;
    private ResultSet rs0  = null;
    private Connection conMySQL = null;
   
    private final String url = "jdbc:microsoft:sqlserver://";

    public DBaccess(String filename){  //constructor
        con = null;
        try {
                con = this.getConnection(filename);
//            System.out.println("constructor filename = "+filename);
        } catch(Exception e){ }
    }
     
    private void connect(String filename){ //makes the connection
        con = null;
        try {
            con = this.getConnection(filename);
//            System.out.println("connect filename = "+filename);
        } catch(Exception e){ }
    }
   
    private String getConnectionUrl(String filename){
        //makes a connection
        String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
        String accessDBURLSuffix = ";DriverID=22;READONLY=false}";
        String databaseURL = accessDBURLPrefix + filename + accessDBURLSuffix;
        return databaseURL;
    }
     
    private java.sql.Connection getConnection(String filename){
        try {  //creates the connection
            try {
                Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver" );
            } catch ( java.lang.ClassNotFoundException e ){
                System.err.println("SQL JDBC Driver not found ... ");
                throw ( e );
            }
           //con = java.sql.DriverManager.getConnection(getConnectionUrl(filename), "", "");
            String db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=TLDATABASEDBM.mdb";
            con = DriverManager.getConnection( db ,"","");
        } catch(Exception e){
            System.err.println("Error Trace in getConnection() : " + e.getMessage());
        }
        return con;
    }

    public void closeConnection(){
        try {
           if(con!=null) con.close();
           con=null;
           System.out.println("closeConnection ");
        } catch(Exception e){ }
    }
0
JorgenAx
Asked:
JorgenAx
  • 4
  • 3
1 Solution
 
MacroShadowCommented:
String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
String db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=TLDATABASEDBM.mdb";

Doesn't the Access 2010 format use the .accdb extension?
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
2010 can open either .mdb or .accdb files, but if you mean you are working with a database that was CREATED in 2010, using the newer .accdb format, then as MacroShadow suggests, you would need to change your connection information. I'm not familiar enough with Java to know what that would be, but perhaps this would help:

http://www.connectionstrings.com/access-2007 seems to suggest that this would work:

Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\mydatabase.accdb;Uid=Admin;Pwd=;
0
 
JorgenAxAuthor Commented:
The file has the extension *.mdb. I have not made the database myself, so it may not be a Access 2010 file, but I am using 2010 on my computer.  I also have windows 7. This did not help me.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
MacroShadowCommented:
If it didn't solve you're problem don't select it as a correct answer!

You may request a moderator to unmark the question as solved.
0
 
MacroShadowCommented:
Are you using a 64bit system?
0
 
JorgenAxAuthor Commented:
I am using 64 bit Windows 7 but Microsoft Office is 32 bit. Netbeans is situated under Program Files so I believe it is running 64 bit
0
 
MacroShadowCommented:
I am using 64 bit Windows 7 but Microsoft Office is 32 bit.
Ok, that explains your problem. The problem is the interaction between 64-bit java app and 32-bit office. You have to either uninstall office 32-bit and use 64-bit (as both cannot be installed side-by-side), or use 32-bit java instead.
0
 
JorgenAxAuthor Commented:
I have teste the program on my old Windows XP computer, and here it works nicely.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now