【问题标题】:DriverManager.getConnection cannot be resolved to a typeDriverManager.getConnection 无法解析为类型
【发布时间】:2015-04-29 20:39:09
【问题描述】:

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

当我尝试编译时,我收到以下错误:

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

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

感谢您的帮助。

【问题讨论】:

  • DriverManager 不需要“新”操作符

标签: java mysql database jdbc


【解决方案1】:

为什么new 在这里?

随便用

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

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

【讨论】:

    【解决方案2】:

    只需像这样编辑您的代码

    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 之后

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 1970-01-01
      • 2021-08-28
      • 2013-12-14
      • 2015-06-10
      • 2015-11-22
      • 1970-01-01
      • 2014-07-23
      • 2011-12-31
      相关资源
      最近更新 更多