JDBC ODBC Connection In Java

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


 

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

JDBC ODBC Connection In Java

Posted on: July 3, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC ODBC Connection In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads