【问题标题】:how to fix SQLException "no suitable driver found for jdbc "? [duplicate]如何修复 SQLException “找不到适合 jdbc 的驱动程序”? [复制]
【发布时间】:2019-08-24 13:43:27
【问题描述】:

我正在尝试连接到我的咖啡数据库以插入数据,但每次我运行它都会显示的代码

No suitable driver found for jdbc:derby://localhost:1527/coffeeZone

我尝试过使用这个 Class.forName("dbc:derby://localhost:1527/coffeeZone"); 我的 Mac 上也有 mysql jdbc 驱动程序

    String query="INSERT INTO `coffeeZone`.`branch` (`branch_Num`, `admin_id`, `Street`, `Nieghbourhood`) VALUES ('?', '?', '?', '?');";

          try{

      PreparedStatement ps;
      String f="jdbc:derby://localhost:1527/coffeeZone [coffee on COFFEE]";

      connection = DriverManager.getConnection(
            f, "coffee", "1234"); 
       ps=connection.prepareStatement(query);
        ps.setString(1, bno);
        ps.setString(2, strt);
       ps.setString(3, nbh);
        ps.setString(4, ci);
   if  (ps.executeUpdate()>0){


                    JOptionPane.showMessageDialog(null, "Complete! a new branch is added !");
               }else
               {
                   JOptionPane.showMessageDialog(null, "User already exists");
               }

    }        catch (SQLException ex) {
           JOptionPane.showMessageDialog(null,ex);
    }                                       

    }

【问题讨论】:

  • "jdbc:derby://", "jdbc:mysql://"。您使用的是哪个 DBMS?
  • 我正在使用 jdbc:derby://
  • Class.forName 需要类名,而不是 uri

标签: java jdbc derby


【解决方案1】:

看起来您需要在建立连接之前调用Class.forName("com.mysql.jdbc.Driver"),基于此连接字符串:jdbc:mysql://localhost/dbname

编辑:如果您使用 Derby,则需要:Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); (https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html)。

您是否发布了您遇到的确切错误?错误指的是 MySql 连接,但代码使用了 Derby 连接字符串,这似乎很奇怪。

【讨论】:

  • OP 说她正在使用 Derby
  • 由于驱动程序的自动加载,不应该使用Class.forName(除非这是一个Web应用程序,但你甚至不应该使用DriverManager)。同样考虑到 URL 包含主机名和端口,EmbeddedDriver 是错误的。
猜你喜欢
  • 2013-06-30
  • 2011-08-24
  • 2014-01-31
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
相关资源
最近更新 更多