我正在为我的程序建立一个登录系统。我正在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);
}
}
1条答案
按热度按时间7rfyedvj1#
你的逻辑是颠倒的。。。
它写着,“如果有结果,显示错误信息,否则打开程序”
改变
if (result.next()) {
至if (!result.next()) {
或者切换每个分支的内容