Home Jdbc JDBC ODBC Connection In Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JDBC ODBC Connection In Java

Advertisement
This section will describe you about the JDBC ODBC Connection In Java.

JDBC ODBC Connection In Java

In this section we will read about the various aspects of JDBC ODBC such as, JDBC-ODBC bridge, JDBC ODBC connection, how to create DSN etc.

JDBC-ODBC Bridge

As its name JDBC-ODBC bridge, it acts like a bridge between the Java Programming Language and the ODBC to use the JDBC API. To use the JDBC API with the existing ODBC Sun Microsystems (Now Oracle Corporation) provides the driver named JdbcOdbcDriver. Full name of this class is sun.jdbc.odbc.JdbcOdbcDriver.

JDBC ODBC Connection

To Connect the JDBC and ODBC we should have a database. Then we would be required to create a DSN to use JDBC ODBC Bridge driver. The DSN (Data Source Name) specifies the connection of an ODBC to a specific server.

How To Create DSN In Java.

To create a DSN we would be required to follow some basic steps. These steps are as follows :

Here I am using the MS-Access as database system. Before creating DSN to use MS-Access with JDBC technology the database file should be created in advance.

  • First step is to go through the Control Panel -> Administrative Tools -> Data Sources (ODBC).
  • Go to the tab System DSN in the ODBC Data Source Administrator dialog box then, Click on Add button -> select MS Access Driver (*.mdb) -> click on Finish button.
  • In the next step a dialog box ODBC Microsoft Access Setup will be opened then provide the field's values corresponding to the field's label (i.e. provide the DSN name as you wish) and then click on the "Select" button.
  • In the next step a new dialog box "Select database" will be opened. Here you have to select the directory where you have stored your file and provide the name in the place of the Database Name and then press ok -> ok -> ok.

Example

Here we are giving a simple example which will demonstrate you about how to use the JDBC API using JDBC ODBC driver. In this example we will use MS-Access as a database. We will create a table and then we will create a DSN by following the steps mentioned above. Then we will create a class in Java and we will make a connection using JDBC ODBC driver and then we will insert the record into the table using SQL query.

JdbcOdbcExample.java

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcOdbcExample {	
	
	public static void main(String args[])
	{
	Connection con = null;
	PreparedStatement stmt = null;	
	try {
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	con = DriverManager.getConnection("jdbc:odbc:swing");
	String sql ="INSERT INTO employee (name, rollNo, course, 

subject, marks) VALUES" +
			"('Deepak', 10, 'MCA', 'Computer Science', 

85)";	
	stmt = con.prepareStatement(sql);
	int i = stmt.executeUpdate();	
	if(i > 0 )
	{
		System.out.println("Record Added Into Table 

Successfully.");
	}
	} catch (SQLException sqle) {
		System.out.println(sqle.getNextException());
	} catch (ClassNotFoundException e) {
		System.out.println(e.getException());
	} finally {
	try {
	if (stmt != null) {
	stmt.close();
	stmt = null;
	}
	if (con != null) {
	con.close();
	con = null;
	}
	} catch (Exception e) {
	System.out.println(e);
	}
	}
	}
}

Output

Table Before Inserting the record into it.

When you will execute the above example you will get the output at console as follows :

And then the Table after inserting record will be seen as follows :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 3, 2013

Ask Questions?    Discuss: JDBC ODBC Connection In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com