f



request for explanation on access an database in ms access

This is the code that I found on the internet for accessing an ms
access database:

import java.sql.*;
class Test


    {
    public static void main(String[] args)


        {


            try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // set this to a MS Access DB you have on your machine
            String filename = "d:/java/mdbTEST.mdb";
            String database = "jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb)};DBQ=";
            database+= filename.trim() +
";DriverID=22;READONLY=true}"; // add on to the end
            // now we can get the connection from the DriverManager
            Connection con = DriverManager.getConnection( database
,"","");
        }


            catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

What I don't understand is the declaration of database. What I did was
putting the database in the same dir as the sourcecode and use
"jdbc:odbc:CafeJolt.mdb". But this doesn't work. And the above code
does, why is that???
0
piggybing (9)
10/11/2004 11:44:31 PM
comp.lang.java.help 13339 articles. 0 followers. Post Follow

2 Replies
3484 Views

Similar Articles

[PageSpeed] 10

Actually it's not Java question. It's ODBC question.
You have to have ODBC connection. Depending of Windows it's usually
something like this (for my Windows 2000):
Start->Sttings->Control Panel->Administartive Tools->Data Sources(ODBC)->
User DSN -> [Add]-> Driver to Microsoft Access(*.mdb)  === And here, finally
you can choose file name.

I have enough of M$ in real life and try to escape it at least in Java confs
:(((

Alex Kizub.

Madhur Ahuja wrote:

> Jean Paul <piggybing@lycos.nl> wrote:
> > This is the code that I found on the internet for accessing an ms
> > access database:
> >
> > import java.sql.*;
> > class Test
> >
> >
> >     {
> >     public static void main(String[] args)
> >
> >
> >         {
> >
> >
> >             try {
> >             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
> >             // set this to a MS Access DB you have on your machine
> >             String filename = "d:/java/mdbTEST.mdb";
> >             String database = "jdbc:odbc:Driver={Microsoft Access
> > Driver (*.mdb)};DBQ=";
> >             database+= filename.trim() +
> > ";DriverID=22;READONLY=true}"; // add on to the end
> >             // now we can get the connection from the DriverManager
> >             Connection con = DriverManager.getConnection( database
> > ,"","");
> >         }
> >
> >
> >             catch (Exception e) {
> >             System.out.println("Error: " + e);
> >         }
> >     }
> > }
> >
> > What I don't understand is the declaration of database. What I did was
> > putting the database in the same dir as the sourcecode and use
> > "jdbc:odbc:CafeJolt.mdb". But this doesn't work. And the above code
> > does, why is that???
>
> Hello
>
> This is not the same thing AFAIK.
>
> First of all, you cannot access the database just with the filename.
> Filename
> is a low level raw thing.
>
> The piece of code above dynamically creates a new DSN for the MS Access
> Database. The name of the driver is clearly specified as
> *Microsoft Access Driver*.
>
> --
> Madhur Ahuja [madhur<underscore>ahuja<at>yahoo<dot>com]
>
> Homepage
> http://madhur.netfirms.com

0
akizub (122)
10/12/2004 1:03:49 AM
Jean Paul <piggybing@lycos.nl> wrote:
> This is the code that I found on the internet for accessing an ms
> access database:
>
> import java.sql.*;
> class Test
>
>
>     {
>     public static void main(String[] args)
>
>
>         {
>
>
>             try {
>             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
>             // set this to a MS Access DB you have on your machine
>             String filename = "d:/java/mdbTEST.mdb";
>             String database = "jdbc:odbc:Driver={Microsoft Access
> Driver (*.mdb)};DBQ=";
>             database+= filename.trim() +
> ";DriverID=22;READONLY=true}"; // add on to the end
>             // now we can get the connection from the DriverManager
>             Connection con = DriverManager.getConnection( database
> ,"","");
>         }
>
>
>             catch (Exception e) {
>             System.out.println("Error: " + e);
>         }
>     }
> }
>
> What I don't understand is the declaration of database. What I did was
> putting the database in the same dir as the sourcecode and use
> "jdbc:odbc:CafeJolt.mdb". But this doesn't work. And the above code
> does, why is that???

Hello

This is not the same thing AFAIK.

First of all, you cannot access the database just with the filename.
Filename
is a low level raw thing.

The piece of code above dynamically creates a new DSN for the MS Access
Database. The name of the driver is clearly specified as
*Microsoft Access Driver*.

--
Madhur Ahuja [madhur<underscore>ahuja<at>yahoo<dot>com]

Homepage
http://madhur.netfirms.com






0
ef (125)
10/12/2004 1:08:58 AM
Reply: