Jdbc connection not established

sam_2012
sam_2012 used Ask the Experts™
on
Hi Team ,
Below is the code Iam using running jdbc application , Iam new to Java . Iam not getting the excpected output


folder NAme : jdbcprograms
in this folder I have ojdbc6.jar and DBConnection.java file and class file.

import java.sql.*;

public class DBConnection
{
      public static void main(String [] args)
      {
            try
            {
      
                  Class.forName("jdbc.odbc.JdbcOdbcDriver");
                  Connection conn = DriverManager.getConnection("jdbc:odbc:Oracle-Hr","HR","orcl");
                  System.out.println(conn.getClass().getName());
            if (conn==null)
            {
                  System.out.println("Connection not established");
            }
            else
            {
                  System.out.println("Connection is  established");
            }
            }
            catch(SQLException e)
            {
                  System.out.println(e);            
            }
            catch(ClassNotFoundException e)      
            {
                  System.out.println(e);            
            }
            catch(Exception e)
            {
                  System.out.println(e);            
            }            
      }
}


When I run the code as below , I get the below output , but not getting the connection established output. Any help is really appreciated

D:\JdbcPrograms>java -jar ojdbc6.jar DBConnection
Oracle 11.2.0.1.0 JDBC 4.0 compiled with JDK6

Expected Output
Class Name Output for Connection Class
Connection established.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
D:\JdbcPrograms>java -jar ojdbc6.jar DBConnection

Open in new window


is the wrong command. It should be

D:\JdbcPrograms>java -cp ojdbc6.jar;. DBConnection

Open in new window

Author

Commented:
D:\JdbcPrograms>java -cp ojdbc6.jar;. DBConnection
java.lang.ClassNotFoundException: jdbc.odbc.JdbcOdbcDriver

getting the above error
Top Expert 2016

Commented:
Is that jar in that directory? If so, it doesn't contain the driver

Author

Commented:
Thanks a lot. I kept the correct jar file in the folder it ran successfully
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial