This section contains the details about creating connection using Jdbc-odbc bridge driver in java.
Connection using Jdbc-odbc bridge Driver
For understanding jdbc, we are taking an example first :
import java.sql.*; public class JdbcOdbc { public static void main(String args[]) { try { Connection con; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:datastud"); try { System.out.println("Getting All Rows from a table!"); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM college"); System.out.println("student_code: " + "\t" + "student_name: "); while (res.next()) { int i = res.getInt("stud_code"); String s = res.getString("stud_name"); System.out.println(i + "\t\t" + s); } con.close(); } catch (SQLException s) { System.out.println("SQL code does not execute."); } } catch (Exception e) { System.out.println("Error:connection not created"); } } }
Output
C:\Program Files\Java\jdk1.6.0_18\bin>javac JdbcOdbc.java C:\Program Files\Java\jdk1.6.0_18\bin>java JdbcOdbc Getting All Rows from a table! student_code: student_name: 1642 ankit 1643 nitesh 1644 rahul |
Description of Program
In the above program, Jdbc-Odbc bridge driver create connection between java application and 'MSAccess database'. After creating connection it also execute query.
Description of code
import java.sql
Since 'java.sql' class support all the
methods needed to create connection. we need to import it first.
Connection con;
Creation of "connection" class object
,which is used to represent connection and also used to create "statement" class
object for executing query.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
In this
program "forName()" function is used to load the JDBC-ODBC bridge driver. It
takes driver name as an argument and load it by checking it's availability.
con=DriverManager.getConnection("jdbc:odbc:datastud");
In
the above code, connection class object 'con' is used by DriverManager to
connect to database using "getConnection()" ."getConnection()" takes url
of database and datasource name. to create connection.
Statement st = con.createStatement();
In this code line
,we create 'Statement' class object using 'con' object of connection. The
Statement object is used to fetch result set by executing query.
ResultSet res = st.executeQuery("SELECT * FROM college");
In
this snippet, statement class object "st" executes query and store result in "ResultSet" class
object "res".
con.close();
It is used to close the connection open by
con object.
Steps to connect JDBC-ODBC bridge driver with database
Step1: First, create a database using "MS Access", which must have the same name ,which you use for query and column name must be same as you use for retrieving values from database.
Step2: Go to Start >control panel >Administrative tool > Data source (ODBC).
Step3: Double click on Data source (ODBC),click on "Add" button, Select MS Access driver...click on "Finish" button.
Step4: Give name to "Data source name". Click on "Select" button and browse your MS access file of ".mbd" extension .Click "OK"
Step5: Now your database is ready to connect using JDBC-ODBC bridge driver. For this you have to mention Data source name in your application.
[ 0 ] Comments