登入界面账号密码是访问数据库,但登入问题时if判断时就是执行不了
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();
}
}