0

Ok, so I have been making an GUI application that should take the information from the User and save it into the sql database. This happens when I click the sign up button on the GUI but for some reason everything is working fine but the data is not showing up in the database. Here's the problem:

My Database Connection file:

package com.Ashmal;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {

public static void main(String[] args) throws ClassNotFoundException, SQLException {

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/genius",
            "root", "$Can$123");
    System.out.println("Connected to DataBase");
}
}

Here's my Configs class:

package DBConnection;

public class Configs {

protected static String dbhost = "localhost";
protected static String dbport = "3306";
protected static String dbuser = "root";
protected static String dbpass = "$Can$123";
protected static String dbname = "genius";
}

Here's my DataBase Handler class:

package DBConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBHandler extends Configs{

Connection dbconnection;
public Connection getConnection() {
    String connectionString = "jdbc:mysql://" + Configs.dbhost + ":" + Configs.dbport + "/"
            + dbname + "?autoReconnect=true&useSSL=false";
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        dbconnection = DriverManager.getConnection(connectionString, Configs.dbuser, dbpass);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return dbconnection;
}
}

Here's my Sign up controller class:

package sample;

import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
import DBConnection.DBHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SignUpController {

@FXML
private AnchorPane parentPane;
@FXML
private Button login;
@FXML
private TextField name;
@FXML
private Button signUp;
@FXML
private RadioButton male;
@FXML
private ToggleGroup gender;
@FXML
private RadioButton female;
@FXML
private RadioButton other;
@FXML
private TextField locationField;
@FXML
private ImageView progress;
@FXML
private PasswordField password;
private Connection connection;
private DBHandler handler;
private PreparedStatement pst;

public void initialize() {
    progress.setVisible(false);
    handler = new DBHandler();
}

@FXML
public void signUP() {
    progress.setVisible(true);
    PauseTransition pt = new PauseTransition();
    pt.setDuration(Duration.seconds(3));
    pt.setOnFinished(e -> System.out.println("Sign Up Successful!"));
    pt.play();

    // Saving Data
    String insert = "INSERT INTO youtubers(names,password,gender,locationField)"
            + "VALUES (?,?,?,?)";
    connection = handler.getConnection();
    try {
        pst = connection.prepareStatement(insert);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        pst.setString(1, name.getText());
        pst.setString(2, password.getText());
        pst.setString(3, getGender());
        pst.setString(4, locationField.getText());
        pst.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public String getGender() {
    String gen = "";
    if (male.isSelected()) {
        gen = "Male";
    } else if (female.isSelected()) {
        gen = "Female";
    } else if (other.isSelected()) {
        gen = "Other";
    }
    return gen;
}
}

Here's the module-info just in case:

module GaveUp {

requires javafx.fxml;
requires javafx.controls;
requires jlfgr;
requires java.sql;
requires mysql.connector.java;
opens sample;
opens img;
opens DBConnection;

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Not directly related to the problem, but the way you handle the exceptions in `signUP` is not a good idea: If an exception happens in the first try-catch the first time you execute the statement on a controller instange, you print the exception and then immendiately get into an NPE, since you're just trying to proceed basically ignoring the exception ever happened except for printing it to the console. Regarding the problem: does calling `Connection.commit` after the update change anything? – fabian Apr 09 '20 at 14:29
  • Regarding the problem: does calling Connection.commit after the update change anything? Can you please explain a little clearly? Thank you. – Ashmal Shoukat Apr 09 '20 at 21:35
  • Not sure how much explanation is needed: https://docs.oracle.com/en/java/javase/13/docs/api/java.sql/java/sql/Connection.html#commit() DB updates silently being rolled back for some reason seems the only way a change could be undone after a successful `executeUbdate` invocation (that is unless you did not properly report the symptoms here or drop the data somewhere else in your code or using a different program...) – fabian Apr 09 '20 at 22:37
  • The main error says that on line 67 of the sign up controller of signUp method, it says nullPointerException on when I wrote: post.setString(1, name.getText()); – Ashmal Shoukat Apr 09 '20 at 22:43
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – fabian Apr 09 '20 at 22:48
  • Getting a stacktrace printed in the console isn't something that I'd expect to be described as *"everything is working fine"*. This kind of information indicates a problem that may be completely unrelated to the db. – fabian Apr 09 '20 at 22:56
  • Does this answer your question? [A Problem related to mysql database and nullpointerexception](https://stackoverflow.com/questions/61150218/a-problem-related-to-mysql-database-and-nullpointerexception) – Mark Rotteveel Apr 11 '20 at 06:24

0 Answers0