Java database connectivity
Back End : MS Access 2003
- Java Application ---> JDBC API ---> JDBC -->ODBC -->Database (MS Access)
- First u need to setup ODBC Connection to ur MS Access Database
- Then u need to connect ur JDBC to ODBC using JdbcOdbcDriver provided with Java
- Setup ODBC : first thing is go to control Panel > Administrative tools > Data Sources ODBC
- Add the database u want from List of databases
- CLick on System DSN Tab
- Add Driver For Respected Database here in out case MS Access !
- Click on Configure > click on Select > select ur DATABASE FILE
- Click on Apply Now u have successfully setup Datasource to ur database!
- 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