public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Enter)
{
String username , password;
username = name.getText();
password = pwd.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ce)
{
JOptionPane.showMessageDialog(ss,ce.getMessage());
}
if(stu.isSelected())
{
try
{
Connection con = DriverManager.getConnection("jdbc:odbc:DS_121301_12","sa","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu where ID='"+username+"' and Pwd='"+password+"'");
while(rs.next())
{
if((rs.getString("ID").equals(username))&&(rs.getString("Pwd").equals(password)))
{
JOptionPane.showMessageDialog(ss,"登陆成功");
Students stu = new Students();
}
else
{
JOptionPane.showMessageDialog(ss,"登录失败");
}
}
首先定义几个字段属性。然后把数据库里面字段值获取出来。然后赋值到字段值里面,然后与文本框里面的值进行比较!注意必须使用:equals()比较、不能使用==