Java用户登录信息的查询,与数据库进行比较匹配代码怎么写?

如题所述

你是想问JDBC 还是只是sql
sql的话
select * (这里最好只查你需要的列) from table t where t.name = ? and t.password = ?;
然后查询 , 不知道是你要的不
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-19
举个列子 贴段代码 运用sql预处理语句
daoLmpl 里面这样写
Connection conn = new ConnectionUtil().openConnection();
String sql="select * from account where alogin=? and apwd=?";
try{
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, account.getAlogin());
pstmt.setString(2, account.getApwd());
ResultSet rs = pstmt.executeQuery();
if(rs.next())
{
temp = new Account();
temp.setAid(rs.getInt("aid"));
temp.setAlogin(rs.getString("alogin"));
temp.setApwd(rs.getString("apwd"));
temp.setAname(rs.getString("aname"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}

Servlet里面这样写
String status = request.getParameter("status"); //隐藏域传过来的
if(status.equals("login"))
{
Account account = new Account();
account.setAlogin(request.getParameter("alogin"));
account.setApwd(request.getParameter("apwd"));
account = accountImpl.queryAccount(account);
if(account == null)
{
request.setAttribute("error","对不起,登录失败!");
request.getRequestDispatcher("/alogin.jsp").forward(request,response);
}else{
request.getSession().setAttribute("account",account);
response.sendRedirect("/shopping/admin/index.jsp");
}
}本回答被提问者采纳
相似回答