简体   繁体   中英

java.lang.ClassCastException error

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)

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);

将您的类重命名为例如MyFirstOracleTest(通过refactor-> rename在Eclipse中完成)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM