JDBC Driver Does Not Exist

Viewed 299

I’m trying to connect a Java program to a remote Oracle DB. After doing some research online, I decided that the easiest way to do this was with the Oracle JDBC driver. I downloaded and ran the jar file and got the message “***** JCE UNLIMITED STRENGTH IS INSTALLED *****.” The problem is that when I try to add the driver to my classpath (javac -classpath ojdbc8.jar Connect.java), I keep getting an error message saying “package oracle.jdbc.driver does not exist.” I’ve been researching how to fix this online, but I’m only getting confused. Any ideas on what I did wrong?

    import java.sql.*;

    public class Class1 {

    public static void main (String args [])
            throws SQLException
    {
        // Load the Oracle JDBC driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

        // Connect to the database
        // You must put a database name after the @ sign in the connection URL.
        // You can use either the fully specified SQL*net syntax or a short cut
        // syntax as `<host>`:`<port>`:`<sid>`.  The example uses the short cut syntax.
        Connection conn =
                DriverManager.getConnection ("jdbc:oracle:thin:hr/hr@myhostname:1521:orcl",
                        "myUsername", "myPassword");

        // Create a Statement
        Statement stmt = conn.createStatement ();

        // Select the ENAME column from the EMP table
        ResultSet rset = stmt.executeQuery ("select ENAME from EMP");

        // Iterate through the result and print the employee names
        while (rset.next ())
            System.out.println (rset.getString (1));

        conn.close(); // ** IMPORTANT : Close connections when done **
    }
}

The error is:

java: package oracle.jdbc.driver does not exist
1 Answers

Can you try to run the sample DataSourceSample.java? Make sure you have the JDBC driver in the classpath. You can also refer to this quickstart for detailed instructions.

Related