Connection Basic between Java and Oracle

1. Create ODBC driver for oracle
if 32bit os and 32 bit oracle no issue.
if 64bit os and 32 bit oracle issues !!!
go to C:\Windows\SysWOW64\odbcad32.exe
then add a new odbc (here i have created JavaOracleODBC) connection for oracle using username and password.


2. Open Eclipse and new project and write the below codes
 
import java.sql.*;

public class dbconnection {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch (ClassNotFoundException e){
            System.out.println(e.getMessage());
        }
        try{
            Connection conection = DriverManager.getConnection("jdbc:odbc:JavaOracleODBC","scott","tiger");
            Statement stmnt = conection.createStatement();
            ResultSet rs = stmnt.executeQuery("select * from emp");
            while(rs.next()){
                String empno = rs.getString(1);
                String ename = rs.getString(2);
                String job = rs.getString(3);
                String mgr = rs.getString("MGR");
                String hiredate = rs.getString(5);
                String sal = rs.getString(6);
                String comm = rs.getString(7);
                String deptno = rs.getString(8);
               
                System.out.println(empno+" "+ename+" "+job+" "
                                    +mgr+" "+hiredate+" "+sal+" "+com+" "+deptno);
            }
            stmnt.close();
            conection.close();
        } catch( SQLException e){
            System.out.println(e.getMessage());
        }
    }

}
 Ref: Issues with ODBC drivers between 32 and 64 bit

Comments

Popular posts from this blog

Restoring Master, Model and MSDB from Netbackup

Oracle Job Operations