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

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?
0
PraKash
Asked:
PraKash
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
objectsCommented:
can u post your current code
0
 
edwardiiiCommented:
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 KwanCommented:
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 KwanCommented:
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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