Java connection to access database

I need to access a mdb database.
I tried using the JDBC-ODBC bridge connector. It keeps giving me "Class not found error"
Is there another way to do that or can someone tell me if there is an external jar file that I can use to fix this error?
PraKashAsked:
Who is Participating?
 
edwardiiiConnect With a Mentor Commented:
Hi, PraKash.  The simple example below works for me on a Win2k Pro system connecting to a MS Access 2000 database. It's necessary to use the code in a Try/Catch block. First, import as follows:

     import java.sql.*;

The code fires when my Jdbc JButton is clicked, pulls all records from my DesignNotes table, and feeds two fields from each record into a JTextArea:

     private void JdbcJButtonActionPerformed(java.awt.event.ActionEvent evt) {
        Connection con;
   
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String MoreTextString;
            con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Reddot_Design_Notes.mdb","","");
           
            Statement pStatement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pStatement.execute("select * from DesignNotes");
            ResultSet rs = pStatement.getResultSet();    
            while (!rs.isLast())
            {
                rs.next();
                MoreTextString = moreJTextArea.getText();
                String Id = rs.getString("ID");
                String Supplier = rs.getString("Supplier");
                moreJTextArea.setText(MoreTextString + "\n" + Id + "  " + Supplier);
            }
            rs.close();
            pStatement.close();
            con.close();            
                   
        }
        catch (SQLException e)
        {
            System.out.println(e.getMessage());
           
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
0
 
objectsCommented:
can u post your current code
0
 
dextCommented:
String strDSN   = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + TestDB;
//testDB is your MDB Filename
Connection cnct = null;
Statement stmt  = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnct = DriverManager.getConnection(strDSN,"","");
stmt = cnct.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
rs.close();
stmt.close();
conn.close();
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
PraKashAuthor Commented:
Hi.
I tried something similar to edwardii. But this is the error that I keep getting.
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

I was also wondering if there was a different way of acecssing a mdb without the jdbc odbc bridge
0
 
edwardiiiCommented:
Hi, PraKash.  If you're using the code in a Try/Catch structure, it should work.  Can you post the code you've got so far, as objects recommended?
0
 
PraKashAuthor Commented:
try{
  Class.forName("org.relique.jdbc.csv.CsvDriver");
     Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + path );
      Statement stmt = conn.createStatement();
      ResultSet results = stmt.executeQuery("SELECT * FROM QUEUE");
  String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};" +
  "DBQ=C:\\example.mdb";
}

In the first statement itself it gives me that error.
0
 
Peter KwanAnalyst ProgrammerCommented:
Hi PraKash,

Did you include the csvjdbc.jar in the classpath?
0
 
PraKashAuthor Commented:
That was a bad example. I pulled the above code from a different project that I was working on. There is no external jar files included for this project.


try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=example.mdb","","");

Its errors out saying sun.jdbc.odbc.JdbcOdbcDriver not found.
Is there any external jar files that I have to include in the classpath?
0
 
Peter KwanConnect With a Mentor Analyst ProgrammerCommented:
Are you using Sun's J2SE when developing and running this program? What is your version of JDK?

It seems you are missing rt.jar in your classloader, which is implicitly loaded in normal.
0
 
edwardiiiCommented:
I agree with pkwan, PraKash.  In fact, are you running this from an IDE, or are you building everything manually with a text editor from the command line?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.