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");
}
}
错误
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)
答案 0 :(得分:3)
重命名您的班级public class Connection
,它与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");
}
需要设置DriverManager.getConnection
的结果应该有一组警钟
(是的,从技术上讲,你可以使用java.sql.Connection con1...
这个仍然是一个坏主意,并且可能继续导致问题没有结束)