Database plays a very important role in maintaining a web site or a organization details.It will be exciting if we are able to use the data present in Database in our program.One of the important features of Java is that we can connect to database using it.Connecting Java to Db is called as Java connectivity.With help of Java connectivity we can retrieve, update the data present in the Database.
For connecting the database to the Java program we need to install the drivers.In our tutorial we going to use Microsoft Access as the database.For installing the drivers we need jdbc-odbc files.Jdbc drivers are available with java api kit.Microsoft provides the odbc drivers to the users defaulty…but we need to install them.To install them follow these steps.
Step 1: Go to control Panel
Step 2: Select administrative tools.
Step 3: Select Data Sources(odbc).
Step 4: There select User DSN…..and click on Add
Step 5: Then select the ms access(*.mdb)
Step 6: Now give the name of the data source which you are going to use.
Step 7: Then click on select button and then find your database…..and then click on ok
With this you installed the drivers for connecting the java the to Ms Access.
Now we shall learn how to write the java code for connecting to the database.The main important steps in any one the database connecting program is:
Step 1: specify the rivers which you are going to use in the program.For this we need to invoke a method called as forName() which is present in the class “Class”.
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Step 2: Now we need to start the connection to the Database.For this we use the method getConnection() which is present in the class DriverManager.Which return an object of connection class.We need to pass the protocol,user id and password for the database.The url consists of three parts protocol,subprotocol,sourcename.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection(url,userid,pswd);
Step 3:Now we need to create an statement to execute the queries.For this we use the method createStatement() which is present in the Connection class and we use the con object to call the method,Which returns an object of statement class.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection(url,userid,pswd);
Statement st = con.createStatement();
Step 4:Now we shall execute the queries on the table.For this we use the method executeQuery() which is present in the class Statement and we use the object st.It returns the set of results of the operation and we need to store them in an object of ResultSet.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection(url,userid,pswd);
Statement st = con.createStatement();
Resultset res = st.executeQuery("select * from emp");
Now the entire code for the program is:
import java.sql.*;
public class msaccessdemo
{
public static void main(String arg[])
{
ResultSet res; //To store the result of the query
String url="jdbc:odbc:company";
String id="";
String pd="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection(url.id,pd);
Statement st = con.createStatement();
res = st.executeQuery("select * from emp");
while(res.next())
{
System.out.print("res.getString(1)+" "+res.getString(2));
}
}
catch(Exception e)
{
System.out.print(e);
}
}
}
OUTPUT:

To download the program click here:download