gpt4 book ai didi

java - 我的程序无法正常工作,为什么它不能正确读取用户名和密码?

转载 作者:行者123 更新时间:2023-11-29 16:36:14 25 4
gpt4 key购买 nike

我正在为我的程序制作一个登录系统。我将帐户信息存储在 MySQL 数据库中。该数据库设置有一个名为“帐户”的表,其中包含两列:用户名和密码。我将 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);
}
}

最佳答案

你的逻辑已经倒退了...

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);
}

关于java - 我的程序无法正常工作,为什么它不能正确读取用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53587701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com