【问题标题】:JDBC connection not happening [duplicate]JDBC连接没有发生[重复]
【发布时间】:2015-09-03 06:33:07
【问题描述】:

下面是我用于jdbc连接的代码

String dbUrl="jdbc:mysql://localhost:3306/mysql";
String user= "kumar";
String pwd="ratiol";

try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
    System.out.println("Database connected!");
} catch (SQLException e) {
    throw new IllegalStateException("Cannot connect the database!", e);
}

但我收到如下错误-

Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
    at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:19)

您能告诉我如何获取 jdbc 网址吗?

我在 ubuntu 14.04 中使用 eclipse(mars)

【问题讨论】:

  • 你的类路径中有 mysql jar 文件吗?
  • 先加载驱动class.forname( com.mysql.jdbc.driver )
  • 只要关注这个tutorial

标签: java mysql eclipse ubuntu jdbc


【解决方案1】:

如果您使用的是netbeans,请右键单击项目-->属性-->库-->添加库并选择MySQL JDBC驱动程序

【讨论】:

    【解决方案2】:

    只需将 com.mysql.jdbc.Driver 添加到程序文件中的 lib 文件夹即可。

    【讨论】:

      【解决方案3】:

      这是你写的

      try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
          System.out.println("Database connected!");
      } catch (SQLException e) {
          throw new IllegalStateException("Cannot connect the database!", e);
      }
      

      应该是这样的:

      Connection con = null;
      try {
          //registering the jdbc driver here, your string to use 
          //here depends on what driver you are using.
          Class.forName("something.jdbc.driver.YourFubarDriver");   
          Connection connection = DriverManager.getConnection(dbUrl, user, pwd)
      } catch (SQLException e) {
          throw new RuntimeException(e);
      }
      

      请查看Class.forName not more needed when using JDBC v.4

      让我们快速了解一下如何使用这个新功能来加载 JDBC 驱动程序管理器。以下清单显示了示例代码 我们通常用来加载 JDBC 驱动程序。假设我们需要 连接到 Apache Derby 数据库,因为我们将在 本文稍后解释的示例应用程序:

       Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
          Connection conn =
              DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
      

      但在 JDBC 4.0 中,我们不需要 Class.forName() 行。我们可以 只需调用getConnection() 即可获取数据库连接。

      请注意,这是为了获得独立的数据库连接 模式。如果您使用某种类型的数据库连接池来管理 连接,那么代码会有所不同。

      No suitable driver found for jdbc:mysql 这样的例外有很多原因

      1. 您的 JDBC URL 可能有误。
      2. ClassPath/BuildPath/Lib 文件夹缺少连接器 jar。
      3. 驱动程序信息有误。

      【讨论】:

      • 您的“更正”代码已过时:不再需要使用 Class.forName(您甚至可以通过引用的文本确认),并且 OP 正确使用了 try-with-resources(假设他们没有'不想对连接做任何其他事情),您的代码通过删除 try-with-resources 来撤消它。
      【解决方案4】:

      只需先加载驱动程序:

      Class.forName("com.mysql.jdbc.driver");
      

      【讨论】:

      • 使用 Class.forName 从 Java 6 / JDBC 4(+ JDBC 4 兼容驱动程序)开始就没有必要了
      猜你喜欢
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 2019-06-28
      • 2018-09-10
      • 2021-12-20
      • 1970-01-01
      相关资源
      最近更新 更多