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 只能保存最后一条记录的值,这就是问题的原因。

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

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!