0

I wish to get a connection between MySQL and my Java application. When I run the program I get this error: my logging information is true. This is a project where I should be able to add, insert, delete and update customer information inside my local database.

java.lang.ClassNotFoundException: com/mysql/jdbc/Driver.class
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at sample.Database.Connection.getConnection(Connection.java:26)
    at sample.Presentation.Controller.<init>(Controller.java:19)
    at sample.Presentation.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

Code

public class Controller {

public Controller(Stage primaryStage) {
    ButtonPane buttonPane = new ButtonPane();
    AddPane addPane = new AddPane();
    VBox vBoxStart = new VBox();
    Connection connection = new Connection();

    try {
        connection = connection.getConnection();
    }catch (Exception e){
        System.out.println("No connection");
    }
    Querys querys = new Querys();


    HBox hBoxOption = new HBox(buttonPane.ButtonPane(),addPane.getPane());// Menu and add/update/delete
    HBox hBoxView = new HBox();  // Calender/tableView

    vBoxStart.getChildren().addAll(hBoxOption,hBoxView);

    Scene scene = new Scene(vBoxStart,1000,1000);
    primaryStage.setScene(scene);
    primaryStage.show();

    Connection finalConnection = connection;
    addPane.getButtonAddCustomer().setOnAction(event ->{
        querys.viewTable(finalConnection,"customer");
    } );

}
}
public class Connection {

String userName = "root";
String password = "rasmus12";
String dbms = "mysql";
String serverName = "localhost";
String portNumber = "3306";

public Connection getConnection()  {

    Connection conn = null;
        try {
            Class.forName("com/mysql/jdbc/Driver.class");
            conn = (Connection) DriverManager.getConnection(
                    "jdbc:" + this.dbms + "://" +
                            this.serverName +
                            ":" + this.portNumber + "/",
                    this.userName,this.password);
            System.out.println("Connected to database");
        } catch (SQLException e) {
            System.out.println("SQL Exception in connection");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    return conn;
}
}

public class Querys {

public void viewTable(Connection con, String dbName) {


    Statement stmt = null;
    String query = "select * " +
            "from " + dbName;
    try {

        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            int id = rs.getInt("person_id");
            String firstName = rs.getString("person_first_name");
            String lastName = rs.getString("person_last_name");
            String adresse = rs.getString("person_adresse");
            String zip = rs.getString("person_zip");
            String city = rs.getString("person_city");
            int mobilePhone = rs.getInt("person_mobile_phone");
            int phone = rs.getInt("person_phone");
            int driverLicenceNumber = rs.getInt("person_driver_licence_number");
            String driverSinceDate = rs.getString("person_driver_since_date");
            String registrationNumber = rs.getString("person_registration_number");
            int orderId = rs.getInt("order_id");
            System.out.println(id + "\t" + firstName +
                    "\t" + lastName + "\t" + adresse +
                    "\t" + zip +
                    "\t" + city +
                    "\t" + mobilePhone +
                    "\t" + phone +
                    "\t" + driverLicenceNumber +
                    "\t" + driverSinceDate +
                    "\t" + registrationNumber+
                    "\t" + orderId);
        }
    } catch (SQLException e) {
        System.out.println("SQL Exception");
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I have removed the IntelliJ tag, as your question is not actually about IntelliJ. That you use IntelliJ to develop your program is not related to this problem. Given this question is caused by a typos/wrong class name (see [answer by YCF_L](http://stackoverflow.com/a/43784715/466862)), I have decided to close your question to the canonical duplicate for connecting the MySQL from Java. – Mark Rotteveel May 04 '17 at 14:31

2 Answers2

2

The driver is not correct you have to use :

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

Instead of this :

Class.forName("com/mysql/jdbc/Driver.class");

But the call to Class.forName() is no longer necessary since Java 6, you can read this, and like @duffymo mention in his answer, make sure that the driver jar exist in your classpath

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

ClassNotFoundException means the class loader cannot find the MySQL JDBC driver class.

You need to download the MySQL Connector JAR and add it to your CLASSPATH.

I'd recommend a MySQL JDBC tutorial.

Here's a Java tip: Don't write messages in catch blocks:

catch (SQLException e) {
    System.out.println("SQL Exception");
}

Print or log the entire stack trace. It's more information that your message provides.

catch (SQLException e) {
    e.printStackTrace();
}
duffymo
  • 305,152
  • 44
  • 369
  • 561