IeuanW
IeuanW

Reputation: 298

How to use an MySQL database to authenticate a user?

I'm trying to authenticate a user (login) in java using MySQL database. I want to be able to do this in the console. This is what i have so far: My main class-

import java.util.Scanner;

public class main {
    public static void main(String args[]){
        if (args.length < 2)
            return;
        login meth = new login();
        Scanner in = new Scanner(System.in);

        String username;
        String password;

        System.out.println("");
        System.out.println("Username: ");
            username = in.next();
        System.out.println("2. Account Holder");
            password = in.next();
        System.out.println("");
        meth.loginDetails(username, password);


        login checker = new login();
        boolean rc = checker.loginDetails(args[0], args[1]);


        System.out.println("The result is: " + Boolean.toString(rc));

        in.close();   
    }

}

And this is my class where i attempt to connect to the database and query it.

import java.sql.*;

public class login {
    public void databaseConnection(){ //swapping index a and index b around
        try     {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = null;
            conn = DriverManager.getConnection("jdbc:mysql://localhost/loginTest","root", "");
            System.out.println("Database is connected !");
            conn.close();
        }
        catch(Exception e){
            System.out.println("Do not connect to DB - Error:"+e);
        }
    }


    static final String CONN_URL = "jdbc:mysql://localhost/loginTest";
    static final String DB_USER = "root";
    static final String DB_PASSWORD = "";

    public boolean loginDetails(String accountNumber, String password){
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        boolean passwdOK = false;

        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(CONN_URL, DB_USER, DB_PASSWORD);

            // Create and execute an SQL statement
            String sqlst = "SELECT Password FROM customer " +
                                 " WHERE Account Number = '" + accountNumber + "'";

            stmt = con.createStatement();
            rs = stmt.executeQuery(sqlst);

            // Check the return data
            if (rs.next()) {
                String passwdSaved = rs.getString(1).trim();
                if (passwdSaved.equals(password.trim()))
                    passwdOK = true;
            }

            rs.close();
            stmt.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (con != null) try { con.close(); } catch(Exception e) {}
        }

        return passwdOK;
    }
}

My database is called Customer and the separate columns are called Account Number, Password.

At the moment when i run the program it gets instantly terminated. Without showing any of the print lines.

So my question is how do i get a result true or false, that the user name and password is a match. And if it was false, let them retry and if its true load a switch menu?

Upvotes: 0

Views: 161

Answers (1)

thedarkpassenger
thedarkpassenger

Reputation: 7348

First of all change

boolean rc = checker.loginDetails(args[0], args[1]);

to

boolean rc = checker.loginDetails(username, password);

If you are not running this program with arguments then args[0] and args[1] will throw ArrayIndexOutOfBoundException

Upvotes: 1

Related Questions