How to get Database Connection using JDBC-ODBC?

Below Java code shows how to get database connection using JDBC-ODBC :

[java]
/****************************************************************************************
* Created on 05-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.javasql;

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

/**
* Created by https://kodehelp.com
* Date: 5/9/11
*/
public class GetJDBCODBCConnection {
public static void main(String args[]) throws ClassNotFoundException, SQLException {
Connection conn =null;
ResultSet rs = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");
rs = conn.createStatement().executeQuery("select empId,empName from Employee");

while (rs.next()) {
System.out.println(rs.getString("empId")+":"+rs.getString("empName"));
}
}catch (ClassNotFoundException cnfEx){
throw cnfEx;
}catch (SQLException sqlEx){
throw sqlEx;
}finally{
if(rs!=null){
rs.close();
}
if(conn!=null){
conn.close();
}
}
}
}
[/java]

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *