注册 登录
编程论坛 JAVA论坛

登入界面账号密码是访问数据库,但登入问题时if判断时就是执行不了

张贤明 发布于 2018-05-23 20:57, 3446 次点击
while(res.next()){
            System.out.println(res.getString("userid"));
            if(drzh.getText().equals(res.getString("userid"))){     //这个老是不执行
            System.out.println(0);
            if((drmm.getText()).equals(res.getString("password"))){
            view.setText("登入成功");
            System.out.println("1");
                }

那个if语句中判断账号正确与否就是不执行


完整程序为
import java.awt.*;
import java.awt.event.*;
import *;
import *;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.*;

public class test{
    public void Dframe(){
    JFrame dr=new JFrame("登入界面");
    dr.setLayout(null);
    dr.setBounds(700,100,200,200);
    Container con1=dr.getContentPane();
    dr.setVisible(true);
   
    JLabel lable1=new JLabel("账号");
    lable1.setBounds(0,20,100,20);
    con1.add(lable1);
   
    final JTextField drzh=new JTextField();
    drzh.setBounds(30,20,100,20);
    drzh.setBackground(Color.yellow);
    con1.add(drzh);
   
    final JLabel lable2=new JLabel("密码");
    lable2.setBounds(0,40,100,20);
    con1.add(lable2);
   
    final JTextField drmm=new JTextField();
    drmm.setBounds(30,40,100,20);
    drmm.setBackground(Color.yellow);
    con1.add(drmm);
   
    final JButton drbt=new JButton("登入");
    drbt.setBounds(40,70,60,20);
    drbt.setBackground(Color.blue);
    con1.add(drbt);
   
    final JLabel view=new JLabel();                                    //创建错误提示框
        view.setBounds(40, 100, 200,21);
        con1.add(view);
   
    drbt.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        Connection con;
        final ResultSet res;
        try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("驱动加载成功");
        con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ADM","sa","sa");
        System.out.println("数据库连接成功");
        String sql="select * from USERINTO";
      PreparedStatement stmt=con.prepareStatement(sql);
        res=stmt.executeQuery();
        while(res.next()){
            System.out.println(res.getString("userid"));
            if(drzh.getText().equals(res.getString("userid"))){
            System.out.println(0);
            if((drmm.getText()).equals(res.getString("password"))){
            view.setText("登入成功");
            System.out.println("1");
                }
                else{view.setText("密码错误");System.out.println("2");}
                break;
            }
            //view.setText("账号不存在");
        
        }

        } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

    }
    public static void main(String args[]){
        test main=new test();
        main.Dframe();
   
    }
}
7 回复
#2
林月儿2018-05-23 21:33
程序代码:

String password = null;
try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    System.out.println("驱动加载成功");
    con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ADM","sa","sa");
    System.out.println("数据库连接成功");
    String sql="select password from USERINTO where userid=?";
    PreparedStatement stmt=con.prepareStatement(sql);
    stmt.setString(1,drzh.getText().trim());
    res=stmt.executeQuery();
    if(res.next()){
        password=res.getString("password");
    }
} catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

if(password==null){
    view.setText("账号不存在");
}else{
    if(password.equals(drmm.getText())){
        view.setText("登入成功");
    }else{
        view.setText("密码错误");
    }
}
     
#3
林月儿2018-05-23 21:34
先试着分离显示和数据库访问的处理
#4
张贤明2018-05-23 23:26
回复 2楼 林月儿
我按着你的方法试了下,可还是这个判断不能执行                    if(password.equals(drmm.getText())){
                                                                       view.setText("登入成功");

完整代码如下:
import java.awt.*;
import java.awt.event.*;
import *;
import *;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.*;

public class test{
    public void Dframe(){
    JFrame dr=new JFrame("登入界面");
    dr.setLayout(null);
    dr.setBounds(700,100,200,200);
    Container con1=dr.getContentPane();
    dr.setVisible(true);
   
    JLabel lable1=new JLabel("账号");
    lable1.setBounds(0,20,100,20);
    con1.add(lable1);
   
    final JTextField drzh=new JTextField();
    drzh.setBounds(30,20,100,20);
    drzh.setBackground(Color.yellow);
    con1.add(drzh);
   
    final JLabel lable2=new JLabel("密码");
    lable2.setBounds(0,40,100,20);
    con1.add(lable2);
   
    final JTextField drmm=new JTextField();
    drmm.setBounds(30,40,100,20);
    drmm.setBackground(Color.yellow);
    con1.add(drmm);
   
    final JButton drbt=new JButton("登入");
    drbt.setBounds(40,70,60,20);
    drbt.setBackground(Color.blue);
    con1.add(drbt);
   
    final JLabel view=new JLabel();                                    //创建错误提示框
        view.setBounds(40, 100, 200,21);
        con1.add(view);
   
    drbt.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        Connection con;
        final ResultSet res;
        String password=null;
        try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("驱动加载成功");
        con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=ADM","sa","sa");
        System.out.println("数据库连接成功");
         String sql="select password from USERINTO "+" where userid=?";
            PreparedStatement stmt=con.prepareStatement(sql);
            stmt.setString(1,drzh.getText());
            res=stmt.executeQuery();
            if(res.next()){
                password=res.getString("password");
            }
        
            con.close();
            res.close();
        } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        
        if(password==null){
            view.setText("账号不存在");
        }else{
            //System.out.println("3"+drmm.getText());
            if(password.equals(drmm.getText())){
                //System.out.println("4"+drmm.getText());
                view.setText("登入成功");
            }else{
                view.setText("密码错误");
            }
        }
        }
    });

    }
    public static void main(String args[]){
        test main=new test();
        main.Dframe();
   
    }
}
#5
林月儿2018-05-24 07:02
不能执行说明条件不满意啊,打印pasdword看看是什么
#6
张贤明2018-05-24 09:37
回复 5楼 林月儿
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
#7
张贤明2018-05-24 09:50
回复 5楼 林月儿
发现问题了,就是数据库表格里的内容后面都是有空格的,所以一直对不上号,但我添加进去得东西每空格,怎么已添加完后再看就有了呢
#8
林月儿2018-05-24 23:40
回复 7楼 张贤明
不是有trim函数吗?去除两边空格就好了呀
1