我正在为我的程序制作一个登录系统。我将帐户信息存储在MySQL数据库中。数据库使用名为“ accounts”的表进行设置,该表具有两列:用户名和密码。我将 test 作为用户名,并将 password 作为密码。输入正确的用户名和密码后,应该会打开实际程序。
这是我的问题。当我输入用户名和密码的任何信息时,程序将打开。不管它是否有效。发生的另一件事是,当我没有设置任何信息时,该程序说它无法连接到数据库。这是连接数据库的问题吗?
package xyz.mrshawn.ezsniper;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class LoginMenu {
private JFrame frmEzsniperLogin;
String username;
String password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginMenu window = new LoginMenu();
window.frmEzsniperLogin.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginMenu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmEzsniperLogin = new JFrame();
frmEzsniperLogin.setTitle("EzSniper : Login");
frmEzsniperLogin.setBounds(100, 100, 353, 300);
frmEzsniperLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmEzsniperLogin.getContentPane().setLayout(null);
JLabel lblWelcomeToEzsniper = new JLabel("Welcome to EzSniper! Please login!");
lblWelcomeToEzsniper.setForeground(Color.RED);
lblWelcomeToEzsniper.setFont(new Font("Sylfaen", Font.PLAIN, 20));
lblWelcomeToEzsniper.setHorizontalAlignment(SwingConstants.CENTER);
lblWelcomeToEzsniper.setBounds(10, 11, 317, 33);
frmEzsniperLogin.getContentPane().add(lblWelcomeToEzsniper);
JLabel usernameLabel = new JLabel("Username:");
usernameLabel.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
usernameLabel.setBounds(10, 68, 82, 26);
frmEzsniperLogin.getContentPane().add(usernameLabel);
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
passwordLabel.setBounds(10, 105, 82, 26);
frmEzsniperLogin.getContentPane().add(passwordLabel);
JTextArea usernameArea = new JTextArea();
usernameArea.setFont(new Font("Monospaced", Font.PLAIN, 13));
usernameArea.setBounds(102, 71, 115, 22);
frmEzsniperLogin.getContentPane().add(usernameArea);
JTextArea passwordArea = new JTextArea();
passwordArea.setBounds(102, 108, 115, 22);
frmEzsniperLogin.getContentPane().add(passwordArea);
JButton loginButton = new JButton("LOGIN");
loginButton.setFont(new Font("Sitka Heading", Font.PLAIN, 40));
loginButton.setBounds(88, 176, 161, 46);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
username = usernameArea.getText();
password = passwordArea.getText();
} catch (NullPointerException ex) {
System.err.println("Please enter your username and password!");
return;
}
Connection con;
PreparedStatement ps;
try {
con = DriverManager.getConnection("jdbc:mysql://sql9.freemysqlhosting.net:3306/sql9268088", "censored", "censored");
ps = con.prepareStatement("SELECT `username`, `password` FROM `accounts` WHERE `username` = ? AND `password` = ?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet result = ps.executeQuery();
if (result.next()) {
JOptionPane.showMessageDialog(null, "Invalid username or password!", "ERROR", JOptionPane.WARNING_MESSAGE);
} else {
CreateGUI.main(null);
System.out.println(username);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Unable to connect to account database!", "ERROR", JOptionPane.WARNING_MESSAGE);
}
}
});
frmEzsniperLogin.getContentPane().add(loginButton);
}
}
答案 0 :(得分:0)
您的逻辑倒退了...
if (result.next()) {
JOptionPane.showMessageDialog(null, "Invalid username or password!", "ERROR", JOptionPane.WARNING_MESSAGE);
} else {
CreateGUI.main(null);
System.out.println(username);
}
它显示为“如果有结果,则显示错误消息,否则打开程序”
将if (result.next()) {
更改为if (!result.next()) {
或切换每个分支的内容
if (result.next()) {
CreateGUI.main(null);
System.out.println(username);
} else {
JOptionPane.showMessageDialog(null, "Invalid username or password!", "ERROR", JOptionPane.WARNING_MESSAGE);
}