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???
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
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