标签:
表示层依赖于业务逻辑层,业务逻辑层依赖于数据访问层
package DB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接帮助类 * @author Administrator * */ public class BaseDao { //=======数据库连接信息======== static String url = "jdbc:oracle:thin:@localhost:1521:orcl"; static String username = "news"; static String password = "news"; static String driverClassName = "oracle.jdbc.driver.OracleDriver"; //获取数据库连接 public static Connection getConnection(){ Connection connection = null; try { //加载驱动 Class.forName(driverClassName); //获取连接 connection = DriverManager.getConnection(url,username,password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } /* * 释放连接 * */ public void close_con(Connection connection,Statement st,ResultSet rs){ try { if(connection!= null){ connection.close(); } if(st!= null){ st.close(); } if(rs!= null){ rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package entity; /** * 用户实体 * @author Administrator * */ public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package Dao; import java.util.List; import entity.User; /** * 用户数据访问层接口 * @author Administrator * */ public interface UserDao { /** * 查询用户是否存在 * @param user * @return */ public boolean findUser(User user); /** * 查询用户列表 */ public List<User> findUserList(); }
package Dao.Impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import DB.BaseDao; import Dao.UserDao; import entity.User; /** * 用户数据访问层实现类 * @author Administrator * 继承数据库连接类 */ public class UserDaoImpl extends BaseDao implements UserDao { //数据库连接对象 Connection conn = null; PreparedStatement stm = null; ResultSet rs = null; /** * 实现查询用户功能 */ public boolean findUser(User user) { //获得连接 conn = this.getConnection(); //编写sql String sql = "select * from news_users where uname = ? and upwd = ?"; try { //得到预编译对象 stm = conn.prepareStatement(sql); stm.setString(1, user.getUsername()); stm.setString(2, user.getPassword()); //执行sql得到结果集 rs = stm.executeQuery(); return rs.next(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //释放连接 this.close_con(conn, stm, rs); } return false; } /** * 查询用户列表 */ public List<User> findUserList() { //获得连接 conn = this.getConnection(); //编写sql String sql = "select * from news_users"; //创建泛型集合 List<User> userlist = new ArrayList<User>(); try { //得到预编译对象 stm = conn.prepareStatement(sql); //执行sql得到结果集 rs = stm.executeQuery(); while(rs.next()){ User user = new User(); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); userlist.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; }finally{ //释放连接 this.close_con(conn, stm, rs); } return userlist; } }
package Biz; import java.util.List; import entity.User; /** * 用户业务逻辑层接口 * @author Administrator * */ public interface UserBiz { /** * 用户登录 * @param user * @return */ public boolean login(User user); /** * 查询用户列表 */ public List<User> finduserList(); }
import Dao.UserDao; import Dao.Impl.UserDaoImpl; import entity.User; /** * 用户业务逻辑实现类 * @author Administrator * */ public class UserBizImpl implements UserBiz { //创建数据访问层对象 UserDao userdao = new UserDaoImpl(); /** * 实现登录 */ public boolean login(User user) { //调用数据访问层查询用户 return userdao.findUser(user); } /** * 查询用户列表 */ public List<User> finduserList() { // TODO Auto-generated method stub return userdao.findUserList(); } }
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="dologin.jsp" method="post"> <p>用户名:<input type="text" name="username" /></p> <p>密 码:<input type="password" name="password" /></p> <input type="submit" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="Biz.UserBiz"%> <%@page import="Biz.Impl.UserBizImpl"%> <%@page import="entity.User"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //调用业务逻辑层 UserBiz userbiz = new UserBizImpl(); //获取表单项 request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); //创建用户实体 User user = new User(); user.setUsername(username); user.setPassword(password); //实现登录 boolean f = userbiz.login(user); if(f){ request.getRequestDispatcher("success.jsp").forward(request,response); }else{ %> <script type="text/javascript"> alert("用户名或密码错误!"); location.href="index.jsp"; </script> <% } %>
标签:
原文地址:http://www.cnblogs.com/wlx520/p/4522950.html