关联数据库servlet

你的登录名是从数据去拿到的

首先你要会基本的使用数据库

然后我们就开始 前面我写了简单的servlet的东西 只要前面理解了 这里就很好理解了

 

我自己的登录页面 是One

LoginC中dopost是

response.setContentType("text/html;charset=utf-8");
String username=request.getParameter("userName");
String password=request.getParameter("passWord");
//下面就是对数据库操作 forName 一般的mysql都是这样写的
Connection connection=null;//第一操作数据库的对象
Statement statement=null;//执行语句
ResultSet rsResultSet=null;//结果记录集 保存的查询的结果
try {

Class.forName("com.mysql.jdbc.Driver").newInstance();
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore","root","");
statement=connection.createStatement();
String sqlString="select*from user where username='"+username+"' and password='"+username+"'";
rsResultSet=statement.executeQuery(sqlString);
//验证
if (rsResultSet.next()) {
HttpSession hsHttpServlet=request.getSession(true);
hsHttpServlet.setMaxInactiveInterval(20);//这里停留的时间 过了就要重新认证
hsHttpServlet.setAttribute("pass", "你的用户名"+username);
response.sendRedirect("Wel?userName="+username);
}else {
response.sendRedirect("One");
}

} catch (Exception e) {
e.printStackTrace();
}
//这里随便说下关闭数据库
finally {
try {
//这里要准寻先开后关的原则
if(rsResultSet!=null) rsResultSet.close();
if(statement!=null) statement.close();
if(connection!=null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}

 

Wel中

response.setContentType("text/html;charset=utf-8");

HttpSession hs=request.getSession(true);
String val=(String)hs.getAttribute("pass");
if(val==null)
response.sendRedirect("One");
String name=request.getParameter("uname");
PrintWriter pw=response.getWriter();
pw.println(name+",you are Welcome!");

 

这样就可以拿到数据库中的数据了

这里我要说的就是你要注意你的命名 登录的命名 和数据中的

 

转载于:https://www.cnblogs.com/jinpei/p/6701965.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值