开发工具 idea MySql
用户表
1.系统登陆
实体类
public class UserInfo { private Integer userCode; private String userName; private String userPwd; private Date lastLoginTime; private boolean isUse; //alt+insert public Integer getUserCode() { return userCode; } public void setUserCode(Integer userCode) { this.userCode = userCode; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public Date getLastLoginTime() { return lastLoginTime; } public void setLastLoginTime(Date lastLoginTime) { this.lastLoginTime = lastLoginTime; } public boolean isUse() { return isUse; } public void setUse(boolean use) { isUse = use; } }
BaseDao
package cn.blog.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 数据访问工具类 * @version 1.1 * @author happy * */ public class BaseDao { // 01. 基础内容的准备 private static final String driver="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql:///wk"; private static final String username="root"; private static final String pwd=""; //02, 接口对象的准备 Connection con=null; PreparedStatement ps=null; public ResultSet rs=null; /** * 01.写一个用户获取到一个连接对象的方法,方法的返回值是Connection类型 * @return 连接对象 * @throws Exception */ public Connection getConnection() throws Exception{ Class.forName(driver); //什么条件下,构建connection对象 if (con==null||con.isClosed()) { con=DriverManager.getConnection(url, username, pwd); } //同志们碰到一个 return con; } /** * 执行查询操作 目的:返回一个读取器 * @param sql sql语句 * @param objs 参数列表 * @return 读取器对象 * @throws Exception */ public ResultSet executeQuery(String sql,Object... objs) throws Exception{ con=getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i+1, objs[i]); } rs= ps.executeQuery(); return rs; } /** * 执行增删该操作 * @param sql sql语句 * @param objs 参数列表 * @return 受影响行数 * @throws Exception */ public int executeUpdate(String sql,Object... objs) throws Exception{ con=getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i+1, objs[i]); } int count = ps.executeUpdate(); return count; } /** * 回收连接资源 * @throws Exception */ public void closeAll() throws Exception{ //倒着回收 if(rs!=null){ rs.close(); } if (ps!=null) { ps.close(); } if(con!=null){ con.close(); } } }
dao层impl实现
package cn.blog.dao.iimpl; import cn.blog.dao.BaseDao; import cn.blog.dao.IUserInfoDAO; import cn.blog.entity.UserInfo; import org.junit.Test; import java.sql.ResultSet; /** * Created by Happy on 2017-06-30. */ public class UserInfoDAOImpl extends BaseDao implements IUserInfoDAO { @Test public void testIsLogin(){ UserInfo info=new UserInfo(); info.setUserCode(11); info.setUserPwd("123"); try { boolean flag = isLogin(info); System.out.println(flag); } catch (Exception e) { e.printStackTrace(); } } public boolean isLogin(UserInfo info) throws Exception { boolean flag=false; //根据用户名和密码查看复合条件的记录有几条》》》》》》》》》》》》》》 String sql="select count(1) as UserCode from userInfo where UserCode=? and UserPwd=?"; Object[] paras={info.getUserCode(),info.getUserPwd()}; ResultSet rs = executeQuery(sql, paras); if (rs.next()){ if (rs.getInt("UserCode")>0){ flag=true; } } return flag; } }
selvlet层
package cn.blog.servlet; import cn.blog.dao.IUserInfoDAO; import cn.blog.dao.iimpl.UserInfoDAOImpl; import cn.blog.entity.UserInfo; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by Happy on 2017-06-30. */ public class UserInfoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //write code here please ,oK? //使用dao实现类 IUserInfoDAO dao=new UserInfoDAOImpl(); String userCode=request.getParameter("userCode"); String userPwd=request.getParameter("userPwd"); UserInfo info=new UserInfo(); System.out.println(userCode+"==================="); info.setUserCode(Integer.parseInt(userCode)); info.setUserPwd(userPwd); try { boolean flag = dao.isLogin(info); if (flag){ request.getRequestDispatcher("/BlogHtTemplate-master/html/main.html").forward(request,response); }else{ response.sendRedirect("/BlogHtTemplate-master/html/index.html"); } } catch (Exception e) { e.printStackTrace(); } // dao.isLogin(); //跳转 } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
实现效果
第一天进程到此结束
时间: 2024-12-12 22:05:39