Muze123
Muze123

Reputation: 21

JDBC SQL error no suitable driver found although i had checked the driver class url and my database url as well as the username and password

public void inputGraph(String name, int Quantity) {
        try(Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/InventoryBase", "Serbesius", "N01094L");) {

        Class.forName("org.apache.derby.jdbc.ClientDriver");
        PreparedStatement prep1 = conn.prepareStatement("SELECT NAME FROM SERBESIUS.GRAPHINT WHERE NAME='"+name+"';");
        boolean rs = prep1.execute();
        if (rs == true) {
            int responseUpdate = JOptionPane.showConfirmDialog(null, "Do you want to update " + name + "?", "Data Already Exist", 
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            switch (responseUpdate) {
                case 0:
                    PreparedStatement prep2 = conn.prepareStatement("UPDATE SERBESIUS.GRAPHINT SET QUANTITY = '"+Quantity+"' WHERE "
                            + " NAME ='"+name+"';" );
                    prep2.executeUpdate();
                    break;
                case 1:
                    JOptionPane.showMessageDialog(null, "We will not update.", "Message", JOptionPane.INFORMATION_MESSAGE);
                    break;

                case -1:
                    /*Leave it here empty*/
                    break;
                default:
                    break;
            }
        } else if (rs == false) {
            PreparedStatement prep3 = conn.prepareStatement("INSERT INTO SERBESIUS.GRAPHINT(NAME, QUANTITY) VALUES('"+name+"', '"+Quantity+"')");
            prep3.executeUpdate();
        }
        conn.close();

    } catch (SQLException s) {
        System.err.println("SQL error: " + s.toString() + s.getSQLState() + s.getErrorCode());

    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe.getMessage());
    }

}

Upvotes: 0

Views: 51

Answers (1)

Mureinik
Mureinik

Reputation: 311338

You need to register the driver (which happens when you force its class to be loaded) before attempting to use it (with DriverManager.getConnection):

// Register the driver first
Class.forName("org.apache.derby.jdbc.ClientDriver");

// Use it
try(Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/InventoryBase", "Serbesius", "N01094L")) {

Upvotes: 1

Related Questions