0

Hi I'm practicing Java oracle database connection with Eclipse.

Below is my code:

package connection;

import java.sql.* ;  // for standard JDBC programs
import java.math.* ; //

public class Connection {

    public static void main(String[] args) throws SQLException {
        try {
               Class.forName("oracle.jdbc.driver.OracleDriver");
            }
            catch(ClassNotFoundException ex) {
               System.out.println("Error: unable to load driver class!");
               System.exit(1);
            }

        String URL = "jdbc:oracle:thin:C##AVIVI/avivi9694@localhost:1521:oracleavivi";
        Connection conn = (Connection) DriverManager.getConnection(URL);

Below is the "ERROR":

Exception in thread "main" java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to connection.Connection
    at connection.Connection.main(Connection.java:18)

I imported all the "jar" file from "jre7'-"lib"folder into my project(I'm a newbie,so didnt quite sure which jar I need so I imported them all,don't know if this will affect the connection or not)

2 Answers 2

2

Connection is ambiguous (for you, not for the compiler) here as this is the name of your main class:

     Connection conn = (Connection) DriverManager.getConnection(URL);
     //^^^^^^^^         ^^^^^^^^^^
     //   connection.Connection, not java.sql.Connection

Either rename your class, or use the fully qualified class interface name:

     java.sql.Connection conn = DriverManager.getConnection(URL);
Sign up to request clarification or add additional context in comments.

2 Comments

There is no need for the cast itself; DriverManager already returns java.sql.Connection.
@Mark Thank you for pointing that out. I've let the cast in my original answer because it was already there in OP's code. But you're right, this is useless. I removed it.
0

Rename your Class to e.g. MyFirstOracleTest (done in eclipse by refactor->rename)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.