java 登录失败 显示在页面上_java设置一个登录界面输入账号密码,与数据库内账号密码相比较,正确显示登录成功反之失败...

展开全部

public class loginServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String username=request.getParameter("user");

String password=request.getParameter("password");

Connection conn=null;

ArrayList users=new ArrayList();

boolean isLogin=false;

try {

String url = "jdbc:mysql://localhost:3306/db_bookstore?"

+ "user=root&password=666666&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC";

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(url);

String sql = "SELECT CustomerName,CustomerPwd from db_bookstore.tb_customerinfo";

Statement stat = conn.createStatement();

ResultSet rs = stat.executeQuery(sql);

while(rs.next()){

User user = new User();

user.setUsername(rs.getString("CustomerName"));

user.setPassword(rs.getString("CustomerPwd"));

users.add(user);

}

rs.close();

stat.close();

} catch (SQLException e) {

e.printStackTrace();

}catch (ClassNotFoundException e2){

e2.printStackTrace();

}

finally {

try {// 关闭连接

if (conn != null) {

conn.close();

conn = null;

}

} catch (Exception ex) {

}

}

for(int i=0;i

User user=(User)users.get(i);

String Username=(String)user.getUsername();

String Password=(String)user.getPassword();

System.out.println(Username);

if(Username.equals(username)&&Password.equals(password))//之前username放在前面时报了异常,估计时觉得username可能没有接受到数据,为空62616964757a686964616fe59b9ee7ad9431333366306433

{

isLogin=true;

}

}

if (isLogin == true) {

response.sendRedirect(request.getContextPath() + "/index.jsp");

} else {

System.out.println("用户不存在");

response.sendRedirect(request.getContextPath() + "/login.jsp");

}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request,response);

}

}

首先是连接数据库的四大操作,这个在网上随便都能找到;然后在进行数据库语言的查询操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值