create database student; use student; create table users( uname varchar(10) primary key, upwd varchar(10) ); insert into users values('admin','1234'); insert into users values('zhou','zhou');package homework7_二_2_20221548; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class Demo { public static void main(String args[]){ MyWindow win=new MyWindow(); } } class MyWindow extends JFrame{ JLabel lbl_name=new JLabel("username"); JLabel lbl_password=new JLabel("password"); JTextField txt_name=new JTextField(15); JTextField txt_password=new JTextField(15); JButton btn1=new JButton("confirm"); JButton btn2=new JButton("cancel"); MyWindow(){ JPanel jp1=new JPanel(); jp1.setLayout(new FlowLayout()); jp1.add(lbl_name); jp1.add(txt_name); jp1.add(lbl_password); jp1.add(txt_password); add(jp1,BorderLayout.CENTER); JPanel jp2=new JPanel(); jp2.setLayout(new FlowLayout()); jp2.add(btn1); jp2.add(btn2); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String txt1=txt_name.getText(); String txt2=txt_password.getText(); String[] username=new String[2]; String[] password=new String[2]; new DemoQuery(username,password); if(txt1.equals(username[0])&&txt2.equals(password[0])) { JOptionPane.showMessageDialog(null,"欢迎您,"+username[0]+"登录"); } else if(txt1.equals(username[1])&&txt2.equals(password[1])){ JOptionPane.showMessageDialog(null,"欢迎您,"+username[1]+"登录"); } else { JOptionPane.showMessageDialog(null,"用户名和密码错误"); txt_name.setText(""); txt_password.setText(""); } } }); add(jp2,BorderLayout.SOUTH); setSize(300,150); setVisible(true); setTitle("the login window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class DemoQuery { public DemoQuery(String[] username,String[] password){ String url = "jdbc:mysql://127.0.0.1:3306/student"; String user = "root", pwd = ""; Connection con; Statement sql; ResultSet rs; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("test" + e); } try { con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String sqlStatement; sqlStatement = "SELECT * From users"; rs = sql.executeQuery(sqlStatement); while (rs.next()) { int i = 0; username[i] = rs.getString("uname"); password[i] = rs.getString("upwd"); System.out.println(username[i]+" "+password[i]); i++; } con.close(); } catch (SQLException e) { System.out.println("数据库操作失败" + e); } } }为什么用户名输入admin,密码输入1234显示登录失败?
时间: 2024-03-17 13:45:39 浏览: 200
适用于EulerOS 2.0 (SP10x86-64)的openssh9.7P1 rpm包
在 DemoQuery 类里的 while 循环中,每次遍历结果集时都会将 username 和 password 数组的第一个元素赋值为当前行的 uname 和 upwd,但是在下一次遍历时,又会将其覆盖为当前行的 uname 和 upwd,因此最终 username 和 password 数组中只保存了最后一行的数据,而不是所有行的数据。因此,在 MyWindow 类中判断用户名和密码是否正确时,只能匹配到最后一行的数据,而无法匹配到第一行的数据。解决方法是将数组的定义和 i 的初始化放在 while 循环之前,每次遍历结果集时让 i 加 1,这样就可以保存所有行的数据了。修改后的 DemoQuery 类代码如下:
```java
class DemoQuery {
public DemoQuery(String[] username,String[] password){
String url = "jdbc:mysql://127.0.0.1:3306/student";
String user = "root", pwd = "";
Connection con;
Statement sql;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("test" + e);
}
try {
con = DriverManager.getConnection(url, user, pwd);
sql = con.createStatement();
String sqlStatement;
sqlStatement = "SELECT * From users";
rs = sql.executeQuery(sqlStatement);
int i = 0;
while (rs.next()) {
username[i] = rs.getString("uname");
password[i] = rs.getString("upwd");
System.out.println(username[i]+" "+password[i]);
i++;
}
con.close();
} catch (SQLException e) {
System.out.println("数据库操作失败" + e);
}
}
}
```
另外,还需要把 MyWindow 类中的判断条件改为:
```java
if(txt1.equals(username[0])&&txt2.equals(password[0])) {
JOptionPane.showMessageDialog(null,"欢迎您,"+username[0]+"登录");
} else if(txt1.equals(username[1])&&txt2.equals(password[1])){
JOptionPane.showMessageDialog(null,"欢迎您,"+username[1]+"登录");
} else {
JOptionPane.showMessageDialog(null,"用户名和密码错误");
txt_name.setText("");
txt_password.setText("");
}
```
因为现在 username 和 password 数组中保存了所有行的数据,所以需要逐一判断每个元素是否与输入的用户名和密码匹配。如果匹配成功,则弹出欢迎对话框,否则弹出错误提示,并清空用户名和密码输入框。
阅读全文