《jsp+servlet+javaBean自我大总结常用模式.doc》由会员分享,可在线阅读,更多相关《jsp+servlet+javaBean自我大总结常用模式.doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、jsp+servlet+javaBean自我大总结常用模式 第一步:建一个操作员表。 create table TOPERATOR ( OPERATOR_ID NVARCHAR2(20) primary key, OPERATOR_NAME VARCHAR2(20), OPERATOR_PWD VARCHAR2(20), IS_ADMIN CHAR(1)第二步:创建javaBean/*o * 操作实体类 * * author chenlijiang 2010-12-1 */public class Operator implements Serializable private static
2、final long serialVersionUID = 1L; public static final int ADMIN = 1; public static final int OPER = 2; private String operator_id; private String operator_name; private String operator_pwd; private String is_admin; public String getOperator_id() return operator_id; public void setOperator_id(String
3、operatorId) operator_id = operatorId; public String getOperator_name() return operator_name; public void setOperator_name(String operatorName) operator_name = operatorName; public String getOperator_pwd() return operator_pwd; public void setOperator_pwd(String operatorPwd) operator_pwd = operatorPwd
4、; public String getIs_admin() return is_admin; public void setIs_admin(String isAdmin) is_admin = isAdmin; 第三步:建立一个类,封装所有数据库操作/* * 把对数据库的操作放在本类中。 update:对数据库的修改操作 select: 查询数据库中的数据 * * author chenlijiang 2010-12-14 */public class DbUtil private final String url; private final String user; private fi
5、nal String password; private final ThreadLocal th = new ThreadLocal(); static try Class.forName(oracle.jdbc.OracleDriver); catch (ClassNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); private DbUtil() url = jdbc:oracle:thin:localhost:1521:orcl; user = system; password = or
6、cl; / 单例模式 private static DbUtil dbUtil; public static DbUtil getDbUtil() if (dbUtil = null) dbUtil = new DbUtil(); return dbUtil; / 获得Connection连接 public Connection getConnection() Connection conn = th.get(); if (conn = null) try conn = DriverManager.getConnection(url, user, password); / conn.setAu
7、toCommit(false); th.set(conn); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); return conn; / 关闭连接 public void closeConnection() Connection conn = th.get(); if (conn != null) try th.remove(); / mit(); conn.close(); System.out.println(closeConnection启动); catch (SQLExcept
8、ion e) / TODO Auto-generated catch block e.printStackTrace(); / 更新数据库方法 public int update(String sql, Object. objects) /此种写法相当有效。以后所有的insert,update,delete方法都可以调用该方法来实现,该方 /法用了一种可变参数(Object.objects)的方式来实现。 Connection conn = getConnection(); try PreparedStatement p = conn.prepareStatement(sql); for (i
9、nt i = 0; i objects.length; i+) p.setObject(i + 1, objectsi); int rows = p.executeUpdate(); p.close(); return rows; catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); try throw new DbException(数据库更新出错); catch (DbException e1) / TODO Auto-generated catch block e1.printStack
10、Trace(); return 0; / 查询所有数据的方法。可以单独列出来实现,大家可能看到有个RowParser这个接口。别着急,这个类是自定义的。后面将给出 public List select(String sql, RowParser parser, Object. param) List list = new ArrayList(); try Connection conn = getConnection(); PreparedStatement p = conn.prepareStatement(sql); for (int i = 0; i . public T unique(
11、String sql, RowParser parser, Object. param) Object obj = null; try Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(sql); for (int i = 0; i param.length; i+) stmt.setObject(i + 1, parami); ResultSet rs = stmt.executeQuery(); if (rs.next() obj = parser.convertToModel
12、(rs); rs.close(); stmt.close(); return (T) obj; catch (SQLException e) e.printStackTrace(); throw new DbException(数据库查询出错); 第三步:附上前面提到的RowParser接口,看清楚了吧,就写了一个方法public interface RowParser Object convertToModel(ResultSet rs) throws SQLException;第四步:定义DAO接口,里面定义了操作数据库的方法,当然你也可以填上delete方法。方便别的类实现他以实现相应的
13、操作public interface IOperatorDao Operator findById(String id); List list(); public void add(Operator oper); public void update(Operator oper);第五步:实现DAO,实现里面的增删查改方法public class OperatorDAO implements IOperatorDao, RowParser /此处才真正实现了前面提到的RowParser接口里的方法。作用就不说了,相信都看得懂的 private final RowParser parser =
14、new RowParser() public Object convertToModel(ResultSet rs) throws SQLException / TODO Auto-generated method stub Operator operator = new Operator(); operator.setOperator_id(rs.getString(operator_id); operator.setOperator_name(rs.getString(operator_name); operator.setOperator_pwd(rs.getString(operato
15、r_passwd); operator.setIs_admin(rs.getString(is_admin); return operator; ; / 添加 public void add(Operator oper) / TODO Auto-generated method stub String sql = insert into toperator(operator_id,operator_name,operator_pwd,is_admin) values(?,?,?,?); DbUtil.getDbUtil().update(sql, oper.getOperator_id(),/
16、调用前面写出的DbUtil类的update方法。此处就为update方法实现了可变参数的 /传参操作 oper.getOperator_name(), oper.getOperator_pwd(), oper.getIs_admin(); / 由ID查询记录,实现方式同上 public Operator findById(String id) String sql = select operator_id,operator_name,operator_pwd,is_admin from toperator where operator_id=?; Operator operator = DbU
17、til.getDbUtil().unique(sql, parser, id); return operator; /查询所有记录,实现方式同上 public List list() / TODO Auto-generated method stub String sql = select operator_id,operator_name,operator_pwd,is_admin from toperator; return DbUtil.getDbUtil().select(sql, parser); / 修改 public void update(Operator oper) / TO
18、DO Auto-generated method stub String sql = update toperator set operator_name=?,operator_pwd=?,is_admin=? where operator_id=?; DbUtil.getDbUtil().update(sql, oper.getOperator_name(), oper.getOperator_pwd, oper.getIs_admin, oper.getOperator_id(); public Object convertToModel(ResultSet rs) throws SQLE
19、xception / TODO Auto-generated method stub return null; 第六步:创建DAO工厂,生产Ddaopublic class DAOFactory private static DAOFactory instance; private DAOFactory() services.put(IOperatorDao.class, new OperatorDAO(); public static DAOFactory getInstance() if (instance = null) instance = new DAOFactory(); retu
20、rn instance; private final Map services = new HashMap(); public T getService(Class clazz) return (T) services.get(clazz); 第七步:定义Service接口,Service的作用其实是调用了DAO的方法变成自己的方法(拿来主义!)public interface IAdminService void addOperator(Operator oper); List listOperator(); Operator findOperatorById(String id); voi
21、d updateOperator(Operator operator);第八步:实现该Service接口,service的作用大家也看到了,主要就是把DAO里面的方法调用过来,变成自己的方法(改个名字而已)大家看到了红色的字(BaseService),我将在第九步给出。大家/* * * * author chenlijiang 2010-12-15 */public class AdminService extends BaseService implements IAdminService private final IOperatorDao operatorDao = daoFactory
22、 .getService(IOperatorDao.class); public void addOperator(Operator oper) / TODO Auto-generated method stub operatorDao.add(oper); public Operator findOperatorById(String id) / TODO Auto-generated method stub return operatorDao.findById(id); public List listOperator() / TODO Auto-generated method stu
23、b return operatorDao.list(); public void updateOperator(Operator operator) / TODO Auto-generated method stub 第九步:BaseServicepublic class BaseService protected DAOFactory daoFactory = DAOFactory.getInstance(); 第十步:生产Servicepublic class ServiceFactory private static ServiceFactory instance; private Se
24、rviceFactory() services.put(IAdminService.class, new AdminService();-(5) ; public static ServiceFactory getInstance() if (instance = null) instance = new ServiceFactory(); return instance; private final Map services = new HashMap(); public T getService(Class clazz) return (T) services.get(clazz); 以上
25、步骤哦我们已经把业务层Service简历起来了。之后的工作就是如何在C(控制器)里调用业务完成我们需要的功能 第十一步:简历Servlet/*author chenlijiang 2010-11-19 处理新增操作员时请求 参见addOperator.jsp */public class AddOperatorServlet extends HttpServlet public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException re
26、quest.setCharacterEncoding(UTF-8); response.setCharacterEncoding(UTF-8); response.setContentType(text); String userid=request.getParameter(userid); String username=request.getParameter(username); String userpass=request.getParameter(userpass); String isAdmin=request.getParameter(isAdmin); private fi
27、nal IAdminService adminSrv = serviceFactory/这就是在Servlet中调用service业务逻辑了 .getService(IAdminService.class); Operator operator=newe Operator(); operator.setOperator_name(username); operator.setOperator_pwd(userpass); operator.setIs_admin(isAdmin); adminSrv.addOperator(operator); request.getRequestDispatcher(./page/managerOperator.jsp).forward(request, response) 最后一步:简历输入端的JSP页面 My JSP addoperator.jsp starting page 登录ID: 姓名: 密码: 密码确认: 是否管理员: 是 否 以上就是大体步骤,写得有点乱。大家体谅下。可以好好看看,这些也是jsp+servlet+javaBean的基本模式了。