0

I did a project using NetBeans 8 where I have to connect to a mysql dataBase and display the content in a table. While I am using the IDE the connection works just fine! But when I clean and build to create an executable jar, My program cannot connect to the dataBase... Could anyone help me with this? Thank you!

That is the code I am using:

@FXML
    private void loadDB() {
        //cleaning all fields from table View and removing previous elements from studentList before show new content;
        tableView.getItems().clear();
        studentList.removeAll();

        String stg = dataBaseField.getText();
        //Expected string format:
        //jdbc:mysql://mymysql.senecacollege.ca/eden_burton?user=jac444_183a01&password=eqXE@4464

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Error loading the driver" + e);
        }
        try {
            //connecting to the database
            Connection conexao = DriverManager.getConnection(dataBaseField.getText());

            //creating a statement and query to be executed
            Statement myStmt = conexao.createStatement();
            ResultSet result = myStmt.executeQuery("select * from Students");

            //look at the result set
            while (result.next()) {
                Student student_ = new Student(result.getInt("id"), result.getString("name"), result.getString("course"), result.getInt("grade"));
                studentList.addNaLista(student_);
                tableView.setItems(getStudent(studentList.getList()));
            }

            myStmt.close();//closing statement
            result.close();//closing results
            conexao.close();//closing connection

        } //if a connection could not be set.
        catch (SQLException exc) {
            alert("CONNECTION ERROR!", "Please verify your connection string.", "String should be in the following format: " + "\n\njdbc:mysql://mymysql.senecacollege.ca/DATABASENAME?user=USERNAME&password=PASSWORD");
        }
    }

2 Answers2

0

Depdending on the discussion in the comments of the question:

The problem is you are missing the MySQL JDBC jar in the JVM's classpath when running the executable jar. It works out of eclipse because the IDE takes care of the correct classpath creation when adding dependencies to the project and running one of it's class files.

If you are creating a jar out of the project, eclipse will not add the dependency to it. You need to handle it yourself. Now you have two options:

  1. Let eclipse bundle the jar into your executable jar like described here
  2. Do the creation of the classpath yourself when running the jar. You need to add the -cp argument to the process call and list the necessary jars for your application to run. (See example here)

EDIT:

Since you are using Netbeans, it seems, that Netbeans does not have the Option like eclipse does. So you are stuck with my Option 2

markusw
  • 1,975
  • 16
  • 28
0

Assuming you are using an Ant based project in Netbeans, you can configure your project so that NetBeans will create a runnable jar file and will copy all dependent libraries to the dist folder of your project.

To do that, enable the checkbox "Copy dependent libraries" in the "Packaging" section (under "Build") of the project's properties.

enter image description here

(The screenshot was taken with NetBeans 10, but older versions have exactly the same options)

Make sure you have a Main Class defined in the Run section of the project.

Once you have done that and you build your project the dist folder will contain MyApp.jar and sub-directory lib that will contain all libraries you have added to your project. The MyApp.jar will contain a MANIFEST.MF that includes your application jar and all dependent libraries (located in the lib directory) so that you can start your application using java -jar MyApp.jar