2

I need to connect an odbc database to my java code. I know that for connecting mdb database I need to use this code but it doesn't work:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "C:/porogram/pro.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 ,"",""); 
Statement s = con.createStatement();

Thank you very much.

rkosegi
  • 14,165
  • 5
  • 50
  • 83
whiteberryapps
  • 1,392
  • 4
  • 16
  • 21
  • 2
    What does not work? Please add more information or a stacktrace. – Kai Jun 08 '12 at 09:19
  • Define `doesn't work`. If you get errors, post them. If you get unusual behaviour, describe it. You need to use your own debugging skills to home in on the problem, and pass that information to us. – MatBailie Jun 08 '12 at 09:27
  • It was just an example this code doesn't suppose to work. – whiteberryapps Jun 08 '12 at 09:47
  • Look at the driver for example... I just don't know what to do.. – whiteberryapps Jun 08 '12 at 09:48
  • I just don't know how to work with odb db – whiteberryapps Jun 08 '12 at 10:14
  • Since one of the tags say SQL, please do try this [link](http://stackoverflow.com/questions/9307363/jdbc-install-and-configuration-with-jse-6-and-tomcat-6-help-please/9307437#9307437) for that and this [example](http://stackoverflow.com/questions/8518770/how-to-connect-to-the-databases-from-netbean-7-0-1/8523417#8523417), as well :-) – nIcE cOw Jun 10 '12 at 09:26

1 Answers1

2

Currently I'm working with jdbc-odbc bridge and this my code works for me at 100%:

this.jdbcUser = PropUtil.getValue(configFile, "jdbc.user");
this.jdbcPass = PropUtil.getValue(configFile, "jdbc.pass");
this.jdbcUrl = PropUtil.getValue(configFile, "jdbc.url");
this.jdbcDriver = PropUtil.getValue(configFile, "jdbc.driver");


//Be sure to load required JDBC driver
Class.forName(jdbcDriver);
dbconn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass);

Properties file:

jdbc.user=db_user
jdbc.pass=db_pass
jdbc.url=jdbc\:odbc\:Driver={Microsoft Access Driver (*.mdb)};DBQ=C\:/Data/data1.mdb
jdbc.driver=sun.jdbc.odbc.JdbcOdbcDriver

Please share more info about your problem like stacktrace or whatever.

rkosegi
  • 14,165
  • 5
  • 50
  • 83