今天也是和大家分享一下,e家园项目流程,如下是具体操作过程:
首页
三层架构实现步骤:
1.显示层(底层)
2.数据访问层 (impl)
3.业务逻辑层(biz)
接口类
public interface IUserDAO { //登录 Users Login(Users u); //查询所有 List<Users> queryUsersAll(); //查询当个 Users getUsersById(int userid); //获取id int getById(); //注册 int ZhuceUsers(Users u); }
接口实现类(impl)
public class UserDaoImpl extends BaseDao implements IUserDAO { //登录 @Override public Users Login(Users u) { String sql = "select * from tb_e_user where username=? and upassword=?"; ResultSet rs = executeQuery(sql, new Object[] { u.getUsername(), u.getUpassword()}); try { if (rs.next()) { return new Users(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)); } } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.myClose(conn, ps, null); } return u; } public static void main(String[] args) { // System.out.println(new UserDaoImpl().Login(new Users("kk","123"))); // System.out.println(new UserDaoImpl().queryUsersAll().size()); // System.out.println(new UserDaoImpl().getUsersById(1)); System.out.println(new UserDaoImpl().ZhuceUsers(new Users("kk", "123"))); } //查询全部 @Override public List<Users> queryUsersAll() { List<Users> g = new ArrayList<Users>(); try { conn = DBHelper.getConn(); String sql = "select * from tb_e_user"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { g.add(new Users(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5))); } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.myClose(conn, ps, rs); } return g; } //查询单个 @Override public Users getUsersById(int userid) { String sql = "select * from tb_e_user where userid=" + userid; ResultSet rs = this.executeQuery(sql); try { if (rs.next()) { return new Users(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)); } } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.myClose(conn, ps, rs); } return null; } //获取id @Override public int getById() { String sql="select nvl(max(userid),0) from tb_e_user"; int n=0; ResultSet rs = this.executeQuery(sql); try { if(rs.next()) { n=rs.getInt(1)+1; } } catch (Exception e) { e.printStackTrace(); } return n; } //注册 @Override public int ZhuceUsers(Users u) { // 调用增删改通用Basedao方法,并返回影响行数。 int n = this.executeUpdate("insert into tb_e_user values(?,?,?,?,?)", new Object[] { u.getUserid(),u.getUsername(),u.getUpassword(),u.getRealname(),u.getSex()}); // 并返回影响行数 return n; }
业务逻辑层(biz)类
public class UserbizImpl implements IUserbiz{ IUserDAO k=new UserDaoImpl(); //登录 @Override public Users Login(Users u) { return k.Login(u); } //获取全部 @Override public List<Users> queryUsersAll() { // TODO Auto-generated method stub return k.queryUsersAll(); } //获取id @Override public int getById() { // TODO Auto-generated method stub return k.getById(); } //注册 @Override public int ZhuceUsers(Users u) { // TODO Auto-generated method stub return k.ZhuceUsers(u); } }
登录功能
代码展示
<!-- 登录区域 --> <c:if test="${empty users }"> <form action="doLogin.jsp" method="post"> <table style="margin-left: 5px;"> <tr> <td>登陆名:<input name="username" style="width: 110px;" /> </tr> <tr> <td>密码 :<input name="password" style="width: 110px;" /></td> </tr> <tr> <td><input type="image" src="images/button01.gif" /> <img onclik="zhuce()" src="images/button02.gif" /></td> </tr> </table> </form> </c:if> <c:if test="${not empty users} "> <div> <p>尊敬的用户,欢迎回来</p> <h5>${users.username}</h5> </div> </c:if>
do登录加载页面
<% response.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); //调用业务逻辑层 Users u = new Users(username, password); IUserbiz iub = new UserbizImpl(); Users n = iub.Login(u); //实现用户登录功能 if (n != null) {//登录成功 session.setAttribute("username", username); out.println("<script>alert('登录成功');location.href='e_JiaYuan.jsp'</script>"); } else {//登录失败 out.println("<script>alert('登录失败');location.href='e_JiaYuan.jsp'</script>"); } %>
注册功能
代码展示:
<div> <form action="dozhuche.jsp" method="post"> 姓名:<input type='text' name='realname' /> <br/><br/> 账号:<input type='text' name='username' /> <br /><br /> 密码:<input type='password' name='upassword' /> <br /><br /> 性别:<input type='radio' name='sex' value="男" />男 <input type='radio' name='sex' value="女" />女 <input type='submit' value='注册' /> </form> </div>
注册加载页面:
<!-- 注册do--> <% //设置编码格式 request.setCharacterEncoding("utf-8"); //实例化业务逻辑层 IUserbiz userzi = new UserbizImpl(); int userid = userzi.getById(); //获取表单元素 String username = request.getParameter("username"); String upassword = request.getParameter("upassword"); String realname=request.getParameter("realname"); String sex=request.getParameter("sex"); //调用注册方法,返回影响行数 int n = userzi.ZhuceUsers(new Users(userid,username,upassword,realname,sex)); //判断 if (n > 0) {//注册成功 request.getRequestDispatcher("Login.jsp").forward(request, response); out.println("<script>alert('注册成功,请你先登录一下');</script>"); } else {//注册失败 response.sendRedirect("e_jiaYuan.jsp"); out.println("<script>alert('注册失败');</script>"); } %>下拉框数据绑定:
<table style="margin-left: 5px;"> <!-- 下拉框 --> <img src="images/line01.gif" style="margin-top: 20px; margin-left: 50px;" /> <img src="images/left01.jpg" /> <form action="doindex.jsp" method="post"> 类别: <select name="se"> <c:if test="${not empty type }"> <option value='0'>请选择</option> <c:forEach var="k" items="${type }"> <option value='${k.typeid }'>${k.typename }</option> </c:forEach> </c:if> </select> <tr> <td>标题:<input name="title" style="width: 110px;" /> </tr> <tr> <td>作者:<input name="author" style="width: 110px;" /> </tr> <tr> <td align="center"><input type="image" src="images/button03.gif" /></td> </tr> </form> </table>Servlet服务器加载页面
<% //设置编码 request.setCharacterEncoding("utf-8"); //分类编号 String id = request.getParameter("se"); int typeid = 0; if (id != null) { typeid = Integer.parseInt(id); } //标题 String title = request.getParameter("title"); if (title == null) { title = ""; } //作者 String author = request.getParameter("author"); if (author == null) { author = ""; } //实例化业务逻辑层 IArtielebiz artiele = new Artieledaobiz(); //调用模糊查询方法 List<Artiele> art = artiele.queryArtieleAll(typeid, title, author); //实例化业务逻辑层 ITypebiz biz1 = new TypebizImpl(); //调用查询所有方法 List<Type> type = biz1.queryTypeAll(); //小小打印一波 //System.out.println(typeid + "====" + title + "+++" + author + "----" + type); //判断 if (type != null) { //保存集合对象到session域对象 session.setAttribute("type", type); } if (art != null) { //保存集合对象到session域对象 session.setAttribute("art", art); } //重定向到首页 response.sendRedirect("e_jiaYuans.jsp"); %>根据文本框输入的内容,显示模糊查询的结果;
<c:if test="${empty type }"> <!-- 引入 --> <jsp:forward page="doindex.jsp"></jsp:forward> </c:if> <c:if test="${not empty type }"> <c:forEach var="type" items="${type }"> <div id="cba" style="border-color: #CCCCCC; border-style: inset; height: 130px; width: 272px; border-width: 1px; float: left;"> <img src="images/cen0${type.typeid }.jpg" width="272px" /> <ul> <c:forEach var="At" items="${art }"> <!-- 判断类别id是否相同 --> <c:if test="${At.ttype == type.typeid }"> <!-- 显示标题 --> <li>${At.title }</li> </c:if> </c:forEach> </ul> </div> </c:forEach> </c:if>