回复 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();
}
}