How to connect Java with Microsoft Access

  1. Install easysoft (download address: http://download.cnet.com/Easysoft-JDBC-ODBC-Bridge/3000-2065_4-10392636.html)
  2. Click ODBC Data Source Adminsitrator
  3. Choose System DSN Tab
  4. Click Add the fill in Data Source Name (ex: “test”, “my_db”)
  5. Click Select and choose the .mdb file then click OK
  6. Click Apply

The code ( and should works ):

import java.sql.*;
public class Testdb
{
public static void main(String[] args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:tes","","");
Statement stmt = con.createStatement();

//di DB saya ada tabel mhs dengan 3 field yaitu npm, nama dan alamat
ResultSet rs = stmt.executeQuery("Select npm, nama, alamat from mhs");
while (rs.next()) {
int x = rs.getInt("npm");
String s1 = rs.getString("nama");
String s2 = rs.getString("alamat");
System.out.println(x + " " + s1 + " " + s2 + "\n");
}
}
catch (Exception e){
System.out.println("Error: " + e);
}

}
}

Thanks to Lucky for sharing the how-to and the code.

Useful links:

  1. http://oreilly.com/catalog/javadata/chapter/ch04.html
  2. http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2
  3. http://www.easysoft.com/applications/microsoft-access/jdbc-odbc.html
  4. http://java.sun.com/docs/books/tutorial/jdbc/overview/index.html
Advertisements