Java connection to access database

  try
          {
            String myDB = new String( "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=E:\\MusicStore.mdb;");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(myDB);    // Create the connection
            return conn;    
          }
          catch(Exception ex)                            
          {
            System.out.println("Couldn't connect to the database...");
            return null;
          }  

Why won't this worK???
richardmojoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sachinwadhwaCommented:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ= C:\Authors.mdb";
;DriverID=22;READONLY=true}";
Connection con = DriverManager.getConnection( database ,"","");
Ralf KlattEmployee in Civil ServiceCommented:
Hi,

The simple example below is an accepted solution at EE and it works 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 the Jdbc JButton is clicked, pulls all records from a 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());
        }


Found at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21407993.html


Best regards,
Raisor
lyonstCommented:
Hi,

You might find this useful..

http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html

Regards,

lyonst

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.