面对对象技术到底是啥?

       共同学习,共同进步。QQ:240814476


      面对对象编程这门课,我刚开始都不知道干什么的,后来慢慢明白了,就是抽象包装,最终达到把代码像是叠衣服一样,一块一块的。

我复习一下以前学的JSP,废话不说了,上图:

package com.DataBase.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.DataBase.connect.JDBCconnect;
import com.DataBase.entity.contacts;

/**
 * @author cantacts 增删改差
 *
 */
public class contactsDAO {
    public boolean addcontacts(contacts contacts) {
        boolean flag=false;
        //增加功能需要执行两个sql语句,一个语句用于获取唯一值做为主键,还有一个语句才是增加语句;
        //String sql_1 = "select SEQ_T_USER.nextVal as id from dual";
        String sql_2 = "insert into tb_contacts(con_id, con_name, con_phone, con_telephone, con_email,com_id)" +
                "values( (select nvl(max(con_id),0)+1 id2 from tb_contacts), ?, ?, ?, ?,?)";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try{
            conn = JDBCconnect.getConnection();
            
            ps = conn.prepareStatement(sql_2);
            ps.setString(1,contacts.getCon_id());
            ps.setString(2, contacts.getCon_name());
            ps.setString(3, contacts.getCon_phone());
            ps.setString(4, contacts.getCon_telephone());
            ps.setString(5, contacts.getCon_email());
            ps.setString(6, contacts.getCom_id());
            //System.out.println(cz.getAmount());
            //System.out.println(""+sql_2);
            ps.executeUpdate();
            flag=true;
        
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            JDBCconnect.closeAll(conn, ps, rs);
        }
        return flag;
    }
    //删除
        public int deletecontacts(String con_id) {
            int result = 0;
            Connection con = null;
            PreparedStatement pstm = null;
            String sql = "delete from tb_contacts where con_id=?";
            try {
                con = JDBCconnect.getConnection();
                pstm = con.prepareStatement(sql);
                pstm.setString(1, con_id);
                result = pstm.executeUpdate();

            } catch (SQLException e) {
                e.printStackTrace();
            }  finally{
                JDBCconnect.closeAll(con, pstm, null);
            }
            
            return result ;
        }
    //查询联系人by  com_id
            public List<contacts> querycontacts(String com_id) {
                StringBuffer sql = new StringBuffer(
                "select * from tb_contacts where com_id=?");        
                Statement st = null;
                ResultSet rs = null;
                Connection con = null;
                List<contacts> list=new ArrayList<contacts>();        
                try{
                    con = JDBCconnect.getConnection();
                     System.out.println("---------------sql="+sql);
                    st = con.createStatement();
                    rs = st.executeQuery(sql.toString()+com_id);            
                    while(rs.next())
                    {
                        contacts temp=new contacts();
                        temp.setCon_id(rs.getString("con_id"));
                        temp.setCon_name(rs.getString("con_name"));
                        temp.setCon_phone(rs.getString("con_phone"));
                        temp.setCon_telephone(rs.getString("con_telephone"));
                        temp.setCon_email(rs.getString("con_email"));
                        temp.setCom_id(rs.getString("com_id"));
                        list.add(temp);
                    }
                }catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    JDBCconnect.closeAll(con, st, rs);
                }        
                return list;
            }
    public int updatecontacts(contacts contacts) {
        String sql = "update tb_contacts " +
        " set con_name=?, con_phone=?, con_telephone=?, con_email=?, com_id=?" +
        " where con_id=?";
        PreparedStatement pstm = null;
        Connection con = null;
        int result = 0;
        try {
            con = JDBCconnect.getConnection();
            pstm = con.prepareStatement(sql);

            pstm.setString(1, contacts.getCon_name());
            pstm.setString(2, contacts.getCon_phone());
            pstm.setString(3, contacts.getCon_telephone());
            pstm.setString(4, contacts.getCon_email());
            pstm.setString(5, contacts.getCom_id());
            pstm.setString(6, contacts.getCon_id());

            result = pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            JDBCconnect.closeAll(con, pstm, null);
        }

        return result;
        
    }
    
}

这是对联系人的增删改查。但是在前台调用的时候并没有直接从DAO这个类里提取方法,而是通过对servlet类调用方法。

代码如下

package com.DataBase.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.DataBase.Dao.contactsDAO;
import com.DataBase.Dao.customDAO;
import com.DataBase.Dao.orderDAO;
import com.DataBase.Dao.pro_detailDAO;
import com.DataBase.Dao.productDAO;
import com.DataBase.Dao.purchaseDAO;
import com.DataBase.entity.contacts;
import com.DataBase.entity.custom;
import com.DataBase.entity.order;
import com.DataBase.entity.pro_detail;
import com.DataBase.entity.product;
import com.DataBase.entity.purchase;
import com.DataBase.service.contactsService;
import com.DataBase.service.customService;
import com.DataBase.service.orderService;
import com.DataBase.service.pro_detailService;
import com.DataBase.service.productService;
import com.DataBase.service.purchaseService;


public class contactsServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String actionDo = req.getParameter("actionDo");
		if("query".equals(actionDo)){//表示进行查询操作
			query(req, resp);
		}else if("add".equals(actionDo)){//表示进行增加操作
			add(req, resp);
		}else if("delete".equals(actionDo)){//表示进行删除操作
			delete(req, resp);
		}else if("toUpdate".equals(actionDo)){//表示进行修改操作
			toUpdate(req, resp);
		}else if("update".equals(actionDo)){//表示进行修改操作
			update(req, resp);
		}
	}

	//查询操作
	protected void query(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String con_id = req.getParameter("con_id");
		String con_name = req.getParameter("con_name");
		String con_phone= req.getParameter("con_phone");
		String con_telephone = req.getParameter("con_telephone");
		String con_email= req.getParameter("con_email");
		String com_id = req.getParameter("com_id");
		//System.out.
		contacts temp = new contacts();
		temp.setCon_id( con_id);
		temp.setCon_name(con_name);
		temp.setCon_phone(con_phone);
		temp.setCon_telephone(con_telephone);
		temp.setCon_email(con_email);
		temp.setCom_id(com_id);
		
		List<contacts> list=null;
		contactsDAO dao=new contactsDAO();
		list=dao.querycontacts(com_id);
		
		req.setAttribute("list", list);
		
		//跳转页面
		//RequestDispatcher rd = req.getRequestDispatcher("ChuZhang2.jsp");
		//rd.forward(req, resp);
	}

	//增加操作
	protected void add(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String con_id = req.getParameter("con_id");
		String con_name = req.getParameter("con_name");
		String con_phone= req.getParameter("con_phone");
		String con_telephone = req.getParameter("con_telephone");
		String con_email= req.getParameter("con_email");
		String com_id = req.getParameter("com_id");
		//System.out.
		contacts temp = new contacts();
		temp.setCon_id( con_id);
		temp.setCon_name(con_name);
		temp.setCon_phone(con_phone);
		temp.setCon_telephone(con_telephone);
		temp.setCon_email(con_email);
		temp.setCom_id(com_id);
		
		contactsDAO dao=new contactsDAO();
		boolean flag=dao.addcontacts(temp);
		String msg = "";
		RequestDispatcher rd = null;
		if(flag){//表示增加成功,应该转到查询结果页面,显示最新的所有记录;
			//我们这里就提示成功就算了,不继续做查询功能了。
			msg = "增加成功";
			req.setAttribute("msg", msg);
			//rd = req.getRequestDispatcher("ChuZhang2.jsp");
		}else{//增加失败,转回到增加页面
			msg = "增加失败";
			req.setAttribute("msg", msg);
			//rd = req.getRequestDispatcher("ChuZhang1.jsp");
		}
		
		rd.forward(req, resp);
	}

	//删除操作
	protected void delete(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String id = req.getParameter("con_id");
		
		contactsService contacts=new contactsService();
		contacts.delete(id);
		
		req.getRequestDispatcher("/customServlet?actionDo=query").forward(req, resp);
	}

	//4、转到修改页面
	public void toUpdate(HttpServletRequest request,HttpServletResponse response)
		throws ServletException,IOException{
//		String id = request.getParameter("com_id");
//
//		customService service = new customService();
//		
//		custom custom = service.getpurchaseById(id);
//
//		request.setAttribute("purchase", purchase);
		//RequestDispatcher rd = request.getRequestDispatcher("/wangjian/wang_index_update.jsp");
		//rd.forward(request, response);
	}

	//修改操作
	protected void update(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		String con_id = req.getParameter("con_id");
		String con_name = req.getParameter("con_name");
		String con_phone= req.getParameter("con_phone");
		String con_telephone = req.getParameter("con_telephone");
		String con_email= req.getParameter("con_email");
		String com_id = req.getParameter("com_id");
		//System.out.
		contacts temp = new contacts();
		temp.setCon_id( con_id);
		temp.setCon_name(con_name);
		temp.setCon_phone(con_phone);
		temp.setCon_telephone(con_telephone);
		temp.setCon_email(con_email);
		temp.setCom_id(com_id);
		
		contactsService contacts=new contactsService();
		contacts.update(temp);
		
		resp.sendRedirect(req.getContextPath()+"/pro_detailServlet?actionDo=query");
		//req.getRequestDispatcher("/CZServlet?actionDo=query").forward(req, resp);
		
	}	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}

大家也许能够注意到,在我调用这个类的时候,这个类只是负责把  POST过来的东西收集起来封装起来,但是调用的是service 这个中间类。

service  就像名字一样  服务,什么服务呢   就是把前台接收到的信息 传递给他想要调用的方法的一个中间类。

package com.DataBase.service;

import java.util.List;

import com.DataBase.Dao.contactsDAO;
import com.DataBase.entity.contacts;



public class contactsService {
	contactsDAO dao = new contactsDAO();

	public contactsService() {
	}

	public boolean addcontacts(contacts contacts) {
		return dao.addcontacts(contacts);
		
	}
	
	public List<contacts> getcontacts(String com_id) {
		return dao.querycontacts(com_id);
	}



	public int update(contacts contacts) {
		return dao.updatecontacts(contacts);
	}


	public int delete(String con_id) {
		return dao.deletecontacts(con_id);
	}
}

这样  这三个类就完成了一系列的操作。

在写后台的时候要注意一定要注意封装和抽象。这点很重要!做一款真正的软件需要大量的抽象和封装,这样可以大大的减少代码的耦合程度。提高代码的利用率



 

       


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值