A Problem regarding saving items into a database from intelliJ

152 views Asked by At

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;

}
0

There are 0 answers