You are on page 1of 6

Tutorial on How to Integrate Microsoft Access and Eclipse on Windows 7 Home Premium 64-bit

1. Download JDK 7 http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u4downloads-1591156.html (Windows x86 (32-bit)), and install.

2. Download Eclipse 32-bit http://www.eclipse.org/downloads/ (Eclipse Classic 3.7.2), and install. You also can choose Eclipse IDE for Java EE Developer or Eclipse IDE for Java Developer if you want to, but make sure that you download the Windows 32-bit version.

3. Create a Microsoft Access database, save and note the location of the database. For example, I have created a database named MyLibraryCatalogue.mdb and saved it at D:\My-ProgrammingStuff.

4. Create DSN of the database: i. Open Computer > C: > Windows > SysWOW64. ii. Double click on application named odbcad32.

iii.

When a pop-up window is open, click on System DSN tab, and then click the Add button.

iv.

Select Microsoft Access Driver (*.mdb, *.accdb), and then click the Finish button.

v.

When a pop-up window is open, type the Data Source Name (DSN) of the database that you have created, and then click the Select button. For example, I want to use MyLibraryCatalogueDSN as the DSN of the database that I have created (i.e. MyLibraryCatalogue.mdb).

vi.

When a pop-up window is open, browse to find the database that you have created, select the database, and click the OK button. For example, since the database that I have created is located at D:\My-Programming-Stuff: First, I select d: in the Drives: box. Then, I click on My-Programming-Stuff in the Directories: box. Finally, I select MyLibraryCatalogue.mdb in the box on the left side of the window, and click the OK button.

vii.

Click the OK button on the previous two windows.

5. Open your Eclipse, create a Java project, and create the following class:
import java.sql.*; public class TestDB { Connection con; Statement st; ResultSet rs; public TestDB() { connect(); } public void connect() { try { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; Class.forName(driver); String db = "jdbc:odbc:MyLibraryCatalogueDSN"; con = DriverManager.getConnection(db); // Getting database info DatabaseMetaData meta = con.getMetaData(); System.out.println("Server name: " + meta.getDatabaseProductName()); System.out.println("Server version: " + meta.getDatabaseProductVersion()); System.out.println(""); st = con.createStatement(); String sql = "select * from MyLibraryBookList"; rs = st.executeQuery(sql); while (rs.next()) { String title = rs.getString("Title"); String author = rs.getString("Author"); String category = rs.getString("Category"); String publisher = rs.getString("Publisher"); String published_year = rs.getString("Published-Year"); System.out.println(title +"\t"+ author +"\t"+ category +"\t"+ publisher +"\t"+ published_year); }

DSN of the database

} catch(Exception ex) { System.out.println("Connect error: "+ex); } } public static void main(String args[]) { new TestDB(); } }

6. Output example:
Server name: ACCESS Server version: 04.00.0000 For One More Day Albom, Mitch Novel-Life Hyperion 2006 Sultan Muhammad Al-Fateh Abdul Latip Talib Islam-Sirah PTS The Da Vinci Code Brown, Dan Novel-Thriller null null The Lost Symbol Brown, Dan Novel-Thriller Bantam Press 2009

null

You might also like