Java database connectivity

Front End : Java
Back End : MS Access 2003

  1. Java Application ---> JDBC API ---> JDBC -->ODBC -->Database (MS Access)
  2. First u need to setup ODBC Connection to ur MS Access Database
  3. Then u need to connect ur JDBC to ODBC using JdbcOdbcDriver provided with Java
  4. Setup ODBC : first thing is go to control Panel > Administrative tools > Data Sources ODBC
  5. Add the database u want from List of databases
  6. CLick on System DSN Tab
  7. Add Driver For Respected Database here in out case MS Access !
  8. Click on Configure > click on Select > select ur DATABASE FILE
  9. Click on Apply Now u have successfully setup Datasource to ur database!
  10. REMEMBER THE NAME OF DATASOURCE ! u will need it later!

Programming Part :

  • Import java.sql.*;
  • Register JDBC Driver :

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  • Setup Connection :

Connection con = DriverManager.getConnection("jdbc:odbc:DATASOURCE",Username,Password);

  • Execute Query :

Statement stm = con.createStatement();

RecordSet rs = stm.executeQuery("QUERY HERE");

  • Extract Data :

while(rs.next()) { }

  • To get Value Of Data Field of type String : rs.getString()
  • To get Value of Data field of type integer : rs.getInt()
  • To get Value of Data Field of type Double : rs.getDouble()
  • rs.close()
  • stmt.close()
  • con.close()

Thats It ! You Have Done !

Happy Databasing!

0 comments:

Post a Comment