JDBC-Odbc Connectivity

The code illustrates an example from JDBC-ODBC Connectivity.


 

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 Connectivity

     

The code illustrates an example from JDBC-ODBC Connectivity. The code helps you in retrieve the name and size of the column. To understand this example we have  a class JdbcOdbcConnectivity ,the class include a main method ( ) follows a list of step in it -

   Importing a package java.sql,provides you a network interface for the interaction between front-end application in java and database in the backend.

   The first step inside the main method ( ) is to load a driver by calling class.forName ( ) by accepting driver class as argument.

   DriverManager.getConnection ( ) :The Driver Manager call getConnection ( ) returns you an object of connection class. This method is used to built a connection between url and database. The Connection object call createStatement ( ),this return you a sql object to send and execute SQL Statements in database. 

  

 executeQuery ( ) : This method return you a record set from a table using select query in sql.The obtained record set is stored in result set.

  get Metadata ( ) : This method return you the property of the column's data type, size, the methods, properties, and parameters of a component associated with object in an application.

  get ColumnCount ( ):  This method return you the number of column in a result set.

Finally the println print the column name

  get Column Display Size ( ): This method return you the designated column's normal maximum width in characters.

In case there exists an exception in try block, the catch block check and handle. the exception.

JdbcOdbcConnectivity.java

import java.sql.*;



public class JdbcOdbcConnectivity {



  static private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

  static private final String connection = "jdbc:odbc:emp";



  public static void main(String args[]) {



  Connection con = null;

  Statement st = null;

  ResultSet rs = null;

  ResultSetMetaData metaData = null;



  try {

  Class.forName(driver);

  con = DriverManager.getConnection(connection);



  st = con.createStatement();



  String sql = "select * from Employees";

  rs = st.executeQuery(sql);

  metaData = rs.getMetaData();



  int rowCount = metaData.getColumnCount();



  System.out.println("Table Name : " +

    metaData.getTableName(rowCount));



  System.out.println("Field\t\t" +"Type");

    System.out.println("--------------------");

  for (int i = 0; i < rowCount; i++) {

  System.out.print( metaData.getColumnName(i+1)+"\t");

 System.out.println(" "+metaData.getColumnDisplaySize(i+1));



  }



  con.close();

  catch (Exception e) {

  System.out.println(e);

  }

  }

}

  

Output

rno	libno
1   	101
2   	102
3   	103
4   	104
5   	105
6   	106
7   	107
8   	108

Download code

Advertisements

Ads
Share on Google+Share on Google+

JDBC-Odbc Connectivity

Posted on: November 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC-Odbc Connectivity  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Anmol Singh
June 26, 2013
query

sir is it true that in odbc all database venders gave thier implementation class to communicate with particular database if it is wrong then tell me the exact logic

Ads

 

Ads