Search code examples
javajdbcderby

how to fix SQLException "no suitable driver found for jdbc "?


I am trying to connect to my coffee database to insert data but every-time I run the code it says

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

iv'e tried using this Class.forName("dbc:derby://localhost:1527/coffeeZone"); I also have the mysql jdbc driver on my mac

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

    }

Solution

  • Looks like you'll need to call Class.forName("com.mysql.jdbc.Driver") prior establishing your connection, based on this connection string: jdbc:mysql://localhost/dbname.

    Edit: If you're using Derby, you need this: Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); (https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html).

    Did you post the exact error that you're getting? It seems strange that the error refers to a MySql connection but the code uses a Derby connection string.