Reputation: 9
package connection;
import java.sql.*;
public class Connection {
public static void main(String[] args) throws Exception
{
Connection con1=null;
String c="oracle.jdbc.driver.OracleDriver";
Class.forName(c);
String url="jdbc:oracle:thin:@LAPTOP-1CQ5FAGB:1521:XE";
String user="system";
String passwd="srpnk";
con1=(Connection) DriverManager.getConnection("jdbc:oracle:thin:@LAPTOP-1CQ5FAGB:1521:XE","system","srpnk");
System.out.println("connected");
}
}
Error
Exception in thread "main" java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to connection.Connection
at connection.Connection.main(Connection.java:17)
C:\Users\Nandhini\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Upvotes: 0
Views: 56
Reputation: 347314
Rename your class, public class Connection
, it's conflicting with java.sql.Connection
...
public class Connection { // <-- This is a bad idea
public static void main(String[] args) throws Exception
{
Connection con1=null;
// ...
con1=(Connection) DriverManager.getConnection("jdbc:oracle:thin:@LAPTOP-1CQ5FAGB:1521:XE","system","srpnk");
System.out.println("connected");
}
The need to case the result of DriverManager.getConnection
should have set of alarm bells
(Yes, technically, you could use java.sql.Connection con1...
bad this is still a bad idea, and could continue to cause no end of issues)
Upvotes: 3