package lib;
public class Constants
{
//用于检测用户合法性的表(即用户帐户信息表)
public static final String userInfoTableName = "userInfo";
public static final String userNameFiledName = "userName";
public static final String passwordFiledName = "password";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package model;
import java.sql.*;
import lib.DBConnector;
import lib.Constants;
public class CheckLogin
{
void check(String userName, String password)
{
String sql;
DBConnector dbc = new DBConnector();
dbc.getConnection();
sql = "select * from " + Constants.userInfoTableName + " where ";
sql += Constants.userNameFiledName + "=" + "'" + userName + "'" + " and ";
sql += Constants.passwordFiledName + "=" + "'" + password + "'";
ResultSet rs = dbc.executeQuery(sql);
try
{
System.out.println(sql);
if(!rs.next())
System.out.println("用户名或密码错误!");
else
System.out.println("OK");
}catch(Exception e){System.out.println("用户名或密码错误!");}
}
public static void main(String[] args)
{
CheckLogin cl = new CheckLogin();
cl.check("yin","123456");
}
}
该博客展示了Java代码实现用户登录合法性检测的过程。定义了常量类存储用户信息表及字段名,在CheckLogin类中通过SQL查询语句,结合数据库连接和执行查询操作,根据结果判断用户名和密码是否正确,并输出相应提示。

2338

被折叠的 条评论
为什么被折叠?



