I came back to Java recently after a significant amount of time with Python, and I'm trying to get comfortable with it again. My first desired project was to make a small application where you can (firstly) log in. I am using mySQL to hold the database of usernames and passwords.
So far I have used Java's swing GUI to make a pop-up box asking for sign-in info. The entered username and password are tested against those in the SQL table.
The problem is that I am using a while loop to test the inputs against the SQL table. So the entered info is checked against every key and value in a Hashmap created from the SQL table.
I am not sure how to "extract" my variable g (the Hashmap) from the while loop and the SQL's try statement so that I can use it after it has been filled with the data from mySQL.
I am using Eclipse (Java Neon).
I've no clue how to "extract" the hashmap, and when I try to have the while loop or try statement return the hashmap, Eclipse informs me that void methods cannot return a value (clearly). However, I can not change the return type from void to HashMap)String, String> because "The return type is incompatible with ActionListener.actionPerformed(ActionEvent e)" and " implements java.awt.event.ActionListener.actionPerformed".
Here is my code:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Hello0 extends JFrame {
private static final long serialVersionUID = 1487932324102279819L;
public static void main(String[] args) {
JFrame frame = new JFrame("Frame Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 200);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Username");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
JButton loginB = new JButton("Login");
loginB.setBounds(10, 80, 80, 25);
panel.add(loginB);
loginB.addActionListener(new ActionListener() {
public HashMap<String, String> actionPerformed(ActionEvent e) {
String username0 = "root";
String password = "javaSQLmy98";
try {
String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
Connection connection = DriverManager.getConnection(url, username0, password);
PreparedStatement stmt0 = connection.prepareStatement("SELECT * from userids");
String pass0 = null;
String user1 = userText.getText().toString();
char[] pass1 = passwordText.getPassword();
pass0 = String.valueOf(pass1);
System.out.println(user1);
System.out.println(pass1);
ResultSet rs = stmt0.executeQuery();
rs = stmt0.executeQuery("SELECT * from userids ");
while (rs.next()) {
String user = rs.getString("username");
String pass = rs.getString("paswrd");
Map<String, String> g = new HashMap<>();
g.put(user, pass);
return g;
// This was the alternative: if(g.keySet().contains("Jacob") && g.values().contains("root")) {
/*This code below was originally outside the while loop but I could not
figure out how to make it work without it being inside, and accessible
to the hashmap g. Now it is being checked each time the while loop
is ran, with a new pair of usernames and passwords on each loop. */
if (user1.equals(user) && pass0.equals(pass)) {
System.out.println("Good!");
}
/*The problem here is that the checker IS inside the loop, so it
tests the input against each of the entries in the SQL table. */
else {
System.err.println("The username or password is incorrect.");
}
}
connection.close();
} catch (Exception e1) {
System.err.println(e1.getMessage());
}
}
});
}
}
Cheers!
If you add a WHERE clause to your query, it will return only the data that matches the criteria. In other words, the query below (obviously with the correct values) will return the specified userid entry if it exists, otherwise it will return an empty result set.
You can then do away with the entire while loop and the HashMap, and the code can be simplified as follows:
Also, as suggested before, if this is anything more than a programming exercise, passwords should be hashed instead of being stored in plain text.