用java做了一个简单的登陆窗口,已经连上数据库,如何用数据库验证登陆的用户名和密码?

2024-11-01 16:24:51
推荐回答(2个)
回答(1):

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,"登录失败");
}

}

回答(2):

首先定义几个字段属性。然后把数据库里面字段值获取出来。然后赋值到字段值里面,然后与文本框里面的值进行比较!注意必须使用:equals()比较、不能使用==