ShenYuM 2021-07-07 01:25 采纳率: 100%
浏览 34
已采纳

eclipse连接数据库问题求解决!

我的期末作业是做一个学工系统,连接数据库来判断账号密码是否正确,本来设置成功了结果现在数据库的表中保存的账号只有最新的才能登陆,其他的都是显示的密码错误,请教一下咋回事啊
代码如下
登陆界面
package xgLogin;
import java.awt.;
import java.awt.event.
;
import java.sql.*;

import javax.swing.*;

import xgSystem.JTreeTest;

public class login extends JFrame {

static JFrame jf;

static JPanel jpan1,jpan2,jpan3;
static JLabel username,password,lspace;
static String a;
static String b;
static JTextField user2;//文本框
static JPasswordField pass2;//密码框
static JButton login,cancel;

public static void go() throws ClassNotFoundException, SQLException{
    Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动程序
    String constr = "jdbc:mysql://localhost:3306/student?useSSL=false";//设定数据库连接字符串
    String user="root";
    String pwd = "root";
    Connection conn = null;
    Statement stmt = null;
    String sql = "";
    ResultSet rs = null;

    try {
         conn = DriverManager.getConnection(constr, user, pwd);
         stmt = conn.createStatement();
         sql = "select * from stuinfo";
         rs = stmt.executeQuery(sql);
         //rs.first();
         while(rs.next()) {
             //String stuid = rs.getString("stuid");
             a = rs.getString(1);
             b = rs.getString(2);
            // int stuage = rs.getInt("stuage");
             //JOptionPane.showMessageDialog(null, stuid+" "+stuname+" "+stuage);
            
         }
         
    } catch (SQLException e) {
        e.printStackTrace();
    }        
    jf=new JFrame("学工系统登录");
    
    jf.setSize(400, 250);
    jf.setLocation(750,300);
    jf.setResizable(false);
    jf.setLayout(new GridLayout(6,1,0,5));
    jpan1=new JPanel();
    jpan2=new JPanel();
    jpan3=new JPanel();
    username=new JLabel("账号");
    password=new JLabel("密码");
    user2=new JTextField(12);
    pass2=new JPasswordField(12);
    login=new JButton("登录");
    cancel=new JButton("退出");
    JTreeTest JT = new JTreeTest();
    login.addMouseListener(new MouseListener() {
        public void mousePressed(MouseEvent e) {
               // TODO Auto-generated method stub
        
            //判断
            if(a .equals(user2.getText())&&b.equals(pass2.getText())) {
                JOptionPane.showMessageDialog(null, "登陆成功!");
                jf.dispose();
                JT.ini();
                
            }
            
            else {
                JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入");
            }
            
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }
    });
            
    cancel.addActionListener(event ->jf.dispose());
    
    jpan1.setSize(200, 50);
    jpan1.add(username);
    jpan1.add(user2);
    
    jpan2.setSize(200, 50);
    jpan2.add(password);
    jpan2.add(pass2);
    
    jpan3.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
    jpan3.add(login);
    jpan3.add(cancel);
    
    lspace=new JLabel();
    jf.add(lspace);
    jf.add(jpan1);
    jf.add(jpan2);
    lspace=new JLabel();
    jf.add(lspace);
    jf.add(jpan3);
    lspace=new JLabel();
    jf.add(lspace);

    jf.setVisible(true);
}

public static void main(String[] args) throws ClassNotFoundException, SQLException{
    
    login ui=new login();
    ui.go();
}



    

}
学工系统界面
package xgSystem;

import javax.swing.;
import javax.swing.event.
;
import javax.swing.tree.*;

import java.awt.;
import java.awt.event.
;
public class JTreeTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ini();
    //JTreeTest ge = new JTreeTest();
}
//class 
public static void ini() {
    JFrame mf = new JFrame();
    mf.setSize(1800,1300);
    mf.setLocationRelativeTo(null);
    mf.setLayout(new FlowLayout(FlowLayout.LEFT,20,30));

    JPanel jp = new JPanel();
    
    
    
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("学工管理系统");
    DefaultMutableTreeNode cj = new DefaultMutableTreeNode("成绩");
    DefaultMutableTreeNode qj = new DefaultMutableTreeNode("请假");
    DefaultMutableTreeNode chuqin = new DefaultMutableTreeNode("出勤");
    DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("c1");
    DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("c2");
    
    
    chuqin.add(c1);
    chuqin.add(c2);
    root.add(cj);
    root.add(qj);
    root.add(chuqin);
    //多个面
    JTree tree = new JTree(root);
    
    tree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent arg0) {
            // TODO Auto-generated method stub
            DefaultMutableTreeNode selnode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            //JOptionPane.showMessageDialog(null,selnode.toString());
            String selstr =selnode.toString();                
            if(selstr.equals("c1"))
            {
                JOptionPane.showMessageDialog(null, "学生请假");
            }
        }
        
    });
    
    jp.add(tree);
    mf.add(jp);
    mf.setVisible(true);
}

}

  • 写回答

1条回答 默认 最新

  • 关注

    14行的SQL语句应该加上用户名和密码的查询条件,while 改为if ,如果为true就是登陆成功了;你现在把所有用户取出来了,但循环的时候a,b 只能保存最后一条记录的值,这就是问题的原因。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。