J2EE DAO妯″紡瑙f瀽锛堜笁锛?

时间:2021-02-19 17:02:10

        鎴戜滑鍏堢湅涓?涓嬬晫闈?

      

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%
	String path=request.getContextPath();
	String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
	<head>
       <base href="<%=basePath %>">
		<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
		<title>娣诲姞娴佸悜鍗曠淮鎶?</title>
</head>
<body>
	<form name="flowCardAddForm" method="post" action="servlet/flowcard/FlowCardServlet" >
       <input type="hidden" name="command" value="add">
       <input type=鈥漵ubmit鈥? id=鈥漛tnAdd鈥? value=鈥濇坊鍔犫??/>
    </form>
</body>
</html>


        涓嬮潰鐪嬩竴涓婼ervlet

        

public class FlowCardServlet extends HttpServlet {
    private FlowCardManager flowCardManager;
	public void init() throws ServletException {
		flowCardManager=(FlowCardManager)getBeanFactory().getServiceObject(FlowCardManager.class);
	}
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
       add(request,response);
}
private void add(HttpServletRequest request,HttpServletResponse response)
	 throws ServletException,IOException
	 {
		 /**
         ***璋冪敤涓氬姟閫昏緫灞備唬鐮佷箣鍓嶇殑鍑嗗浠g爜***
		 */
		 //閫氳繃宸ュ巶鍙栧緱涓氬姟閫昏緫瀵硅薄
		 FlowCardManager flowCardManager=(FlowCardManager)getBeanFactory().getServiceObject(FlowCardManager.class);
	flowCardManager.addFlowCard(flowCard);		 response.sendRedirect(request.getContextPath()+"/flowcard/flow_card_maint.jsp");
	 }
protected BeanFactory getBeanFactory()
	{
        BeanFactory beanFactory=BeanFactory.getInstance();
		return beanFactory;
	}
}


        涓嬮潰鐪嬩竴涓嬩笟鍔¢?昏緫灞?

       

public interface FlowCardManager {
   public void addFlowCard(FlowCard flowCard) throws ApplicationException;
}
public class FlowCardManagerImpl implements FlowCardManager {
	private FlowCardDao flowCardDao;

	public FlowCardManagerImpl()
	{
		this.flowCardDao=(FlowCardDao)BeanFactory.getInstance().getDaoObject(FlowCardDao.class);
	}
public void addFlowCard(FlowCard flowCard) throws ApplicationException {
    Connection conn=null;
		try{
			//鍙栧緱Connection
			conn=ConnectionManager.getConnection();
			//寮?濮嬩簨鍔?
			ConnectionManager.beginTransaction(conn);
			//娣诲姞鍓嶇殑鍑嗗宸ヤ綔锛岀粰鍙傛暟璧嬪??***************鐪佺暐
			//娣诲姞娴佸悜鍗曚富淇℃伅
			flowCardDao.addFlowCardMaster(flowCardVouNo, flowCard);
			
			//鎻愪氦浜嬪姟
			ConnectionManager.commitTransaction(conn);
		}catch(DaoException e)
		{
			//鍥炴粴浜嬪姟
			ConnectionManager.rollbackTransaction(conn);
			throw new ApplicationException("娣诲姞娴佸悜鍗曞け璐?");
		}finally
		{
			//鍏抽棴Connection骞朵粠ThreadLocal娓呴櫎
			ConnectionManager.closeConnection();
		}
}
}

        鏈?鍚嶥ao灞?

       

public interface FlowCardDao {
	
	/**
	 * 娣诲姞娴佸悜鍗曚富淇℃伅
	 * @param flowCardVouNo
	 * @param flowCard
	 */
	public void addFlowCardMaster(String flowCardVouNo,FlowCard flowCard);
}
public class FlowCardDaoImpl implements FlowCardDao {
		@Override
	public void addFlowCardMaster(String flowCardVouNo, FlowCard flowCard) {
		StringBuffer sbSql=new StringBuffer();
		sbSql.append("鈥︹?︹?︹?︹?︾渷鐣モ?︹?︹?︹?︹?︹?︹??..");
		PreparedStatement pstmt=null;
		try{
			Connection conn=ConnectionManager.getConnection();
			pstmt=conn.prepareStatement(sbSql.toString());
			pstmt.setString(鈥︿负sql璇彞涓渶瑕佺殑鍙傛暟璧嬪?尖?︹?︹??.);
			pstmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("FlowCardDaoImpl-->>addFlowCardMater,exception:"+e);
			throw new DaoException(e);
			
		}finally{
			ConnectionManager.close(pstmt);  //姝ゅ骞舵病鏈夊叧闂璫onnection
		}

	}
}

           鏁翠釜DAO妯″紡灏辨槸杩欐牱瀛愪簡銆?