JDBC SQL错误没有找到合适的驱动程序虽然我检查了驱动程序类URL和我的数据库URL以及用户名和密码

时间:2016-10-27 16:48:22

标签: sql jdbc

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

}

1 个答案:

答案 0 :(得分:1)

您需要在尝试使用驱动程序之前注册驱动程序(在强制加载类时会发生这种情况)(使用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")) {