1

I have built a working program that takes login details logs in and pushes you to a new screen. So now Im looking to tidy it up by moving all the database connections and functions to a MySQLController Class of its own.

This is my MySQLController Class

package testing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class MySQLController {

   public boolean loginDB(String uName, String pWord){

    boolean pass = false;
    System.out.println( "SELECT * FROM Users WHERE USERNAME= " + "'" + uName + "'" 
            + " AND PASSWORD= " + "'" + pWord + "'");

    try{


        String driver = "com.mysql.jdbc.Driver";
        String dbURL = "jdbc:mysql://localhost:3306/projectdb";
        String dbUsername = "root";
        String dbPassword = "happy123";
        Class.forName(driver);

        Connection conn = DriverManager.getConnection(dbURL, dbUsername, dbPassword);

        Statement stmt = conn.createStatement();

        String SQLAccessor = "SELECT * FROM superuser WHERE Username= " + "'" + uName + "'" 
            + " AND Password= " + "'" + pWord + "'";

        ResultSet rs = stmt.executeQuery(SQLAccessor);

        while(rs.next()){
            if(rs.getString("USERNAME") != null && rs.getString("PASSWORD") != null){
                String username = rs.getString("USERNAME");
                System.out.println("USERNAME = " + username);
                String password = rs.getString("PASSWORD");
                System.out.println("PASSWORD = " + password);
                pass = true;
            }
        }
        rs.close();
        stmt.close();
        conn.close();


    } catch(Exception e){
        System.out.println(e);
        System.exit(0);
    }
    return pass;


  }


}

And this is my Login Menu Class

package testing;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;


public class FXMLDocumentController implements Initializable {

@FXML
private Button loginButton;

@FXML
private Label loginFail;

@FXML
private Button registerButton;

@FXML
private TextField usernameField;

@FXML 
private TextField passwordField;

MySQLController DBCon;


//login screen start on completion will take to menu
@FXML
private void loginAction(ActionEvent event) throws IOException {
    Parent menuPage_parent = FXMLLoader.load(getClass().getResource("FXMLMenu.fxml"));
    Scene menuPage_scene = new Scene(menuPage_parent);
    Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

    if(DBCon.loginDB(usernameField.getText(), passwordField.getText()))
    {
        //takes to menu.
        app_stage.hide();
        app_stage.setScene(menuPage_scene);
        app_stage.show();
    }
    else
    {
        //error incorrect details.
        usernameField.clear();
        passwordField.clear();
        loginFail.setText("Incorrect Login");
    }


}

//takes you to registration
@FXML
private void registerAction(ActionEvent event) throws IOException
{
    Parent registerPage_parent = FXMLLoader.load(getClass().getResource("FXMLRegistration.fxml"));
    Scene registerPage_scene = new Scene(registerPage_parent);
    Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    app_stage.hide();
    app_stage.setScene(registerPage_scene);
    app_stage.show();

}


@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

However it throws a InvocationTargetException caused by calling DBCon.loginDB(usernameField.getText(), passwordField.getText()) in the If Statement. I cant seem to figure out why this is happening and its driving me crazy

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.IllegalAccessError: tried to access method projectui.MySQLController.loginDB(Ljava/lang/String;Ljava/lang/String;)Z from class projectui.FXMLDocumentController
at projectui.FXMLDocumentController.loginAction(FXMLDocumentController.java:58)
... 58 more
CryptiK
  • 27
  • 9
  • Please [edit] your question to include the full stack trace. Where are you initializing `DBCon`? – James_D Oct 16 '16 at 15:07
  • @James_D added. I am initialising it in the login Menu Class under the last FXML Text field – CryptiK Oct 16 '16 at 15:11
  • I don't see anywhere where you initialize that field. – James_D Oct 16 '16 at 15:13
  • @James_D Login Menu Class after the initialisation of the password field MySQLController DBCon; is there – CryptiK Oct 16 '16 at 15:15
  • All I see is a declaration. I don't see an initialization. I would expect a null pointer exception, though; not sure why you have an `IllegalAccessError`: I recommend cleaning and rebuilding the project. – James_D Oct 16 '16 at 15:17
  • @James_D Well the MySQLController is initialised in its own file as. Then the declaration is made in the Login screen file so I can access the login function. It seems like it should work but is just doesnt.. – CryptiK Oct 16 '16 at 15:20
  • What do you mean "is initialized in its own file as"? There is nowhere in your code where you set `DBCon` to any value. – James_D Oct 16 '16 at 15:20
  • @James_D well DBCon is an object of the class MySQLController, there are no values to be initialised. I should be just declaring it. Maybe I am misunderstanding here I'm still fairly new to java coming from C++ – CryptiK Oct 16 '16 at 15:23
  • "DBCon is an object of the class MySQLController". But you never create an object, as far as I can see. Though, again, not sure why you are getting the exception you're getting and not a null pointer exception. – James_D Oct 16 '16 at 15:26
  • So how would I go about creating it? AFAIK thats how its created in C++ maybe if I fixed this it would work? – CryptiK Oct 16 '16 at 15:27
  • Same way you create any object: `DBCon = new MySQLController();`. – James_D Oct 16 '16 at 15:31
  • @James_D wow Thank you I completely forgot about that and its exactly the same as C++ It works now. I cant believe I didnt realise that early I feel extremely dumb : – CryptiK Oct 16 '16 at 15:34

0 Answers0