We help IT Professionals succeed at work.

Jdbc connection not established

sam_2012
sam_2012 asked
on
114 Views
Last Modified: 2019-02-05
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

CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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

getting the above error
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
:)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.