Greeting to everyone.
I'm currently making Java restaurant project using MVC for portfolio. Right now, I'm stuck at login form. My problem is I forget how to logging in as a user (Admin or Customer) in MySQL.
In my login form, there are username textfield, password field, login and exit button.
There's no user type because there's only one admin. I don't want to deal with multiple admin.
The name of database is restaurant. Right now, there's only one table: user. There are some data I manually inserted.
After login successful, view will change to Admin or User depending on user type.
These are ControllerLogin and Database. The database I copy paste from another project from my classmate.
public class ControllerLogin extends MouseAdapter implements ActionListener {
private ViewLogin view;
private Database db;
public ControllerLogin() {
view = new ViewLogin();
db = new Database();
view.addActionListener(this);
view.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source.equals(view.getBtnLogin())) {
btnLoginActionPerformed();
}
if (source.equals(view.getBtnExit())) {
btnExitActionPerformed();
}
}
public void btnLoginActionPerformed() {
}
public void btnExitActionPerformed() {
System.exit(0);
}
}
public class Database {
private Connection conn;
private Statement stmt;
private ResultSet rs;
public void connect() {
String url = "jdbc:mysql://localhost/restaurant";
String user = "root";
String pass = "";
try {
conn = DriverManager.getConnection(url, user, pass);
stmt = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
Edit 1:
I searched video how to make login form and found one. I tried to code it on my program, but it's still wrong said that "java.lang.NullPointerException."
public void btnLoginActionPerformed() {
db.connect();
String sql = "Select * from user where username=? and password=?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, view.getUsername());
pst.setString(2, view.getPassword());
rs = pst.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, "Welcome");
} else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Access Denied", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}