DriverManager.getConnection 无法解析为类型

DriverManager.getConnection cannot be resolved to a type

我正在尝试将 Java 程序连接到本地主机中的数据库。我的问题看起来很简单,但是我找不到任何答案。

当我尝试编译时,出现以下错误:

DriverManager.getConnection cannot be resolved to a type

关于以下代码:

try{
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex){
    System.out.println("Error found " + ex);
    ex.printStackTrace();
}

// Here is where the error pops up
Connection connection = new DriverManager.getConnection("jdbc:mysql//localhost:3306/project3", "root", "root");

我假设我搞砸了一些安装,但我不完全确定是什么。

感谢您的帮助。

为什么new在这里?

只需使用

DriverManager.getConnection("jdbc:mysql//localhost:3306/project3", 
                                                            "root", "root");

getConnection()static 方法。就像上面那样调用它。

只需像这样编辑您的代码

try{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex){
System.out.println("Error found " + ex);
ex.printStackTrace();
}


Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/project3", "root", "root");

避免 new 并在 jdbc:mysql

之后放置 :