This discussion is archived
1 2 Previous Next 23 Replies Latest reply: Mar 20, 2012 8:30 AM by EJP RSS

Driver setup failure

924198 Newbie
Currently Being Moderated
I am trying to write a program that needs to connect to an Oracle database on a different machine but I cannot even get my test app to compile.

I know that I need a jdbc driver. I downloaded ojdbc6.jar from the Oracle site. I know that it needs to be in my path. I have copied the file to a number of directories that I know are in my path and even created a directory for it and added that directory to the path. Unfortunatley when I compile I get the followiing error:

OracleTest.java:6: package oracle.jdbc.dirver does not exit
new oracle. jdbc.driver.OracleDriver()

Here is the portion of the test code that the compiler is complaining about. It is taken from the numorous examples on the web.

import java.sql.*;

class OracleTest {
public static void main(String args[]) throws SQLException {
DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver()
);


I must be missing something that is so common knowledge that a newbie like me doesn't know about and everyone else takes for granted that everyone knows it.

I even tried unpacking the jar file without any difference in the error message.

Thanks for all your help!!!!!

Edited by: 921195 on Mar 15, 2012 10:28 AM
  • 1. Re: Driver setup failure
    EJP Guru
    Currently Being Moderated
    DriverManager.registerDriver(
    new oracle.jdbc.driver.OracleDriver()
    );
    You haven't need that step at all since about 2004.
  • 2. Re: Driver setup failure
    924198 Newbie
    Currently Being Moderated
    Thanks for the help. Apparently the internet needs an "Update" button.

    After searching for a "newer" example of a connection I came across this.

    import java.sql.*;

    class OracleTest2 {
    public static void main(String args[]) throws ClassNotFoundException, SQLException
         {
              Class.forName("oracle.jdbc.driver.OracleDriver");

         Connection conn =
              DriverManager.getConnection("URL", "username", "password");
         
         conn.setAutoCommit(false);
         Statement stmt = conn.createStatement();
         ResultSet rset =
              stmt.executeQuery("select BANNER from SYS.V_$VERSION");
         while (rset.next()) {
              System.out.println (rset.getString(1));
         }
         stmt.close();
         System.out.println ("Ok.");
    }
    }

    It compiles but I am getting this new error that still relates to the driver.

    Exception in thread "main" java.sql.SQLException: No suitable driver found for j
    dbc:oracle:thin:@IP:1521:SID
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at OracleTest.main(OracleTest.java:12)

    My googling tells me I still have a driver problem but no solution. Interestingly, I think that the connection actually worked because it returned my SID even though it is not included in the code.

    Anymore insight???
  • 3. Re: Driver setup failure
    EJP Guru
    Currently Being Moderated
              Class.forName("oracle.jdbc.driver.OracleDriver");
    You don't need that either. Just build a correct URL for the database you are using and the correct driver will be found if the JAR is in your CLASSPATH when you connect.
  • 4. Re: Driver setup failure
    rp0428 Guru
    Currently Being Moderated
    >
    I downloaded ojdbc6.jar from the Oracle site. I know that it needs to be in my path
    >
    No - it needs to be in your CLASSPATH.
  • 5. Re: Driver setup failure
    924198 Newbie
    Currently Being Moderated
    I removed the Class.forName line and put the ojdbc6.jar file in my CLASSPATH and still no joy. I get the simular error minus the URL and SID as last stated.

    Edited by: 921195 on Mar 16, 2012 7:54 AM
  • 6. Re: Driver setup failure
    gimbal2 Guru
    Currently Being Moderated
    921195 wrote:
    I removed the Class.forName line and put the ojdbc6.jar file in my CLASSPATH and still no joy. I get the same error as last stated.
    How did you add it to your classpath?
  • 7. Re: Driver setup failure
    924198 Newbie
    Currently Being Moderated
    To test it out I copied the ojdbc6.jar file to a directory that was already stated in the CLASSPATH. I figured if that worked I would create a special folder for such cases and add that folder to my CLASSPATH as well.
  • 8. Re: Driver setup failure
    rp0428 Guru
    Currently Being Moderated
    EJP said you don't need this line? Did you remove it?
    >
    Class.forName("oracle.jdbc.driver.OracleDriver");
    >
    If so, make sure you are importing the driver class. I noticed that you are using a full reference which makes me wonder if you never imported the class to begin with. You don't need that line but you do need to import the driver.
    import oracle.jdbc.driver.OracleDriver;
    Confirm that you are importing the driver.
  • 9. Re: Driver setup failure
    924198 Newbie
    Currently Being Moderated
    Good catch. I was not importing the driver after I removed the Class.forName line.

    But I am still doing something wrong. After adding the line to import the dirver:

    import java.sql.*;
    import oracle.jdbc.driver.OracleDriver;

    I now get this error when compiling.

    OracleTest2.java:2: package oracle.jdbc.driver does not exist
    import oracle.jdbc.driver.OracleDriver;
    ^
    1 error

    The arrow is actually pointing to the last dot between driver and OracleDriver. (This will change when the page is posted).
  • 10. Re: Driver setup failure
    gimbal2 Guru
    Currently Being Moderated
    921195 wrote:
    To test it out I copied the ojdbc6.jar file to a directory that was already stated in the CLASSPATH.
    That classpath entry might just have been bogus already though... Was it something like:

    c:/java/projects/lib/*

    Where the * is the important part, telling Java 6 and up to include all jars in the folder on the classpath?
  • 11. Re: Driver setup failure
    924198 Newbie
    Currently Being Moderated
    I created a new folder at:

    c:\Jfiles\Projects

    I put the ojdbc6.jar file into that folder.

    c:\JFiles\Projects>dir
    Volume in drive C has no label.
    Volume Serial Number is D695-A189

    Directory of c:\JFiles\Projects

    03/16/2012 10:54 AM <DIR> .
    03/16/2012 10:54 AM <DIR> ..
    03/13/2012 12:26 PM 2,714,189 ojdbc6.jar
    1 File(s) 2,714,189 bytes
    2 Dir(s) 596,019,490,816 bytes free

    My classpath is:

    c:\JFiles>echo %classpath%
    C:\Program Files (x86)\eInstruction\CPS PowerPoint Add-in\;c:\Jfiles\Projects

    My current test code is:

    import java.sql.*;
    import oracle.jdbc.driver.OracleDriver;

    class OracleTest2 {
    public static void main(String args[]) throws ClassNotFoundException, SQLException
         {
              //Class.forName("oracle.jdbc.driver.OracleDriver");

         Connection conn =
              DriverManager.getConnection("URL", "username", "password");
         
         conn.setAutoCommit(false);
         Statement stmt = conn.createStatement();
         ResultSet rset =
              stmt.executeQuery("select BANNER from SYS.V_$VERSION");
         while (rset.next()) {
              System.out.println (rset.getString(1));
         }
         stmt.close();
         System.out.println ("Ok.");
    }
    }

    Here is the error I now receive when trying to compile.

    OracleTest2.java:2: package oracle.jdbc.driver does not exist
    import oracle.jdbc.driver.OracleDriver;
    ^
    1 error

    Thanks again for all of your patience and help!!!!
  • 12. Re: Driver setup failure
    rp0428 Guru
    Currently Being Moderated
    Well several people have mentioned your classpath and you still haven't posted it.
    We can't help you solve a classpath problem if you won't show us the classpath being used.

    That import shouldn't even be needed but adding it shows that it can't find the path so you still have a classpath problem.
    Always start by reviewing the docs - JDBC Dev Guide
    http://docs.oracle.com/cd/B28359_01/java.111/b31224.pdf
  • 13. Re: Driver setup failure
    924198 Newbie
    Currently Being Moderated
    Again, thanks for your help.

    Please note that I did post my classpath my last post.

    My classpath is:

    c:\JFiles>echo %classpath%
    C:\Program Files (x86)\eInstruction\CPS PowerPoint Add-in\;c:\Jfiles\Projects

    I will check out your link. Thanks.
  • 14. Re: Driver setup failure
    gimbal2 Guru
    Currently Being Moderated
    So yeah, its wrong. Because there is no * at the end of the entry to tell Java to include all jars in the directory like I already mentioned. Basically that classpath entry is either the root of package folders with .class files, or it is just there to be totally useless.

    Change the classpath to this to be more exact:

    C:\Program Files (x86)\eInstruction\CPS PowerPoint Add-in\;c:\Jfiles\Projects;c:\Jfiles\Projects\ojdbc6.jar

    the proper way of setting up a classpath that always works is to include the jar files themselves in it.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points