DriverManager.getConnection 无法解析为类型

Sam*_* I. 2 java mysql database jdbc

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

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

DriverManager.getConnection 无法解析为类型

在以下代码中:

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");
Run Code Online (Sandbox Code Playgroud)

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

感谢您的任何帮助。

Ruc*_*era 5

为什么new在这里?

只需使用

DriverManager.getConnection("jdbc:mysql//localhost:3306/project3", 
                                                            "root", "root");
Run Code Online (Sandbox Code Playgroud)

getConnection()static方法。就像上面那样称呼它。