What's the right way in Java to connect to a Microsoft Access 2007 database?

本小妞迷上赌 提交于 2019-11-30 16:28:58

To answer your general question I would say the best way to handle database connections in Java is to avoid the JDBC-ODBC bridge. Its OK for testing or learning about JDBC but not for real production use. Also, if you have a data source that does not have its own JDBC driver but it has an ODBC driver then you may not have a choice.

The main reason why I suggest that you stay away from it though is that it makes it difficult to deploy your application. You have to set up the Data Source on the machine that you are running your application from. If you have access to the machine no problem, but suppose you are sending the application off to the client? The pure Java JDBC driver works better for this because it is included as part of your application so once your application is installed it is ready to connect to the data source.

Of course depending on your requirements there are two other types of drivers but thats another discussion.

In general, the best way to work with an RDBMS in Java is by using a JDBC driver that is designed to connect directly to the database. Using the JDBC-ODBC bridge tends to be sloww.

If you are trying to do basic read/write manipulations with an Access database, I would also recommend taking a look at the Jackcess library.

  1. Go to control panel -- > Administrative tool --> ODBC Data Source Administrator
  2. Add database --> Select "Microsoft Driver(*.mdb, *.accdb)"
  3. Dobule click on new database --> Under "Database" click on "select" --> Select your *.accdb file which you hv created as MS access database.
  4. Say OK and go to your java code
  5. Use: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:filename");

It will surely resolve all your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!