分享

struct加Ajax(2)

 WindySky 2007-01-22

com.san.strutsAjax.business.CusotmerDAO (DAO接口)

package com.san.strutsAjax.business;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: huxinsheng
 * Date: 2006-4-9
 * Time: 12:52:47
 * To change this template use File | Settings | File Templates.
 */
public interface CusotmerDAO {
    public void insertCustomer(Object obj)throws Exception;
    public List queryAllCustomer()throws Exception;
    public List queryCustomerByCondition(Map condition)throws Exception;
    public void deleteCustomer(Map condition)throws Exception;
}

 

 

com.san.strutsAjax.business.CustomerDAOImpl(DAO实现)

package com.san.strutsAjax.business.impl;

import com.san.strutsAjax.business.CusotmerDAO;
import com.san.strutsAjax.utils.ConnectionDB;
import com.san.strutsAjax.utils.EngenderID;
import com.san.strutsAjax.forms.CustomerForm;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: huxinsheng
 * Date: 2006-4-9
 * Time: 12:54:26
 * To change this template use File | Settings | File Templates.
 */
public class CustomerDAOImpl implements CusotmerDAO {
    Logger log = Logger.getLogger(CustomerDAOImpl.class);
    Connection conn = null;
    PreparedStatement pstmt;

    {
        pstmt = null;
    }

    ResultSet res = null;


    String sql = null;


    public void insertCustomer(Object obj) throws Exception {
        CustomerForm form = (CustomerForm) obj;
        int id = EngenderID.engenderTableId("CUSTOMERS");
        System.out.println("" + form);
        sql = "INSERT INTO CUSTOMERS VALUES(?,?,?,?,?,?)";

        try {
            conn = ConnectionDB.ConnectToDb();
            conn.setAutoCommit(false);

            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.setString(2, form.getName());
            pstmt.setInt(3, form.getAge());
            pstmt.setInt(4, form.getTelephone());
            pstmt.setString(5, form.getAddress());
            pstmt.setInt(6, form.getPostcode());
            pstmt.executeUpdate();
            conn.commit();
            log.info("保存数据成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            log.info("关闭连接");
            conn.close();
        }

    }

    public List queryCustomerByCondition(Map condition) throws Exception {
        Integer id = Integer.parseInt((String) condition.get("id"));
        System.out.println(id);
        String sql = "SELECT * FROM CUSTOMERS WHERE ID=?";
       // System.out.println(sql);
        try {
            conn = ConnectionDB.ConnectToDb();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            res = pstmt.executeQuery();
            return getResultDTO(res);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("业务层数据访问出错: " + e.getMessage());
            return null;
        } finally {
            log.info("关闭连接");
            conn.close();
        }
    }

    public List queryAllCustomer() throws Exception {
        String sql = "SELECT * FROM CUSTOMERS";
        try {
            conn = ConnectionDB.ConnectToDb();
            pstmt = conn.prepareStatement(sql);
            res = pstmt.executeQuery();
            return getResults(res);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("业务层数据访问出错: " + e.getMessage());
            return null;
        } finally {
            log.info("关闭连接");
            conn.close();
        }
    }

    public List getResultDTO(ResultSet res) throws Exception {
        List results = new ArrayList();
        CustomerForm customer;
        if (res.next()) {
            customer = new CustomerForm();
            customer.setId(res.getInt("ID"));
            customer.setName(res.getString("CUSTNAME"));
            customer.setAge(res.getInt("AGE"));
            customer.setTelephone(res.getInt("TELEPHONE"));
            customer.setAddress(res.getString("ADDRESS"));
            customer.setPostcode(res.getInt("POSTCODE"));
            results.add(customer);
        }
        System.out.println(results);
        return results;
    }

    public List getResults(ResultSet res) throws Exception {
        List results = new ArrayList<CustomerForm>();
        CustomerForm customer;
        while (res.next()) {
            customer = new CustomerForm();
            customer.setId(res.getInt("ID"));
            customer.setName(res.getString("CUSTNAME"));
            customer.setAge(res.getInt("AGE"));
            customer.setTelephone(res.getInt("TELEPHONE"));
            customer.setAddress(res.getString("ADDRESS"));
            customer.setPostcode(res.getInt("POSTCODE"));
            results.add(customer);
        }
        return results;
    }

    public void deleteCustomer(Map condition) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多