consigneedao.java

来自「Java企业级开发项目实践,这是他的附光盘」· Java 代码 · 共 210 行

JAVA
210
字号
//---------------------------------------------------------
// Application: eShop of Network
// Author     : eSingle
// File       : ConsigneeDAO.java
//
// Copyright 2002 LandSoft Corp.
// Generated at Mon Nov 18 21:48:18 CST 2002
// Created by caoguangxin
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------

package com.landsoft.eshop.dao;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;

import com.landsoft.eshop.model.*;
import com.landsoft.eshop.util.CacheManager;

public class ConsigneeDAO extends DAO {

  public ConsigneeDAO(DataSource ds) {
    super(ds);
  }

  public void insert(Consignee consignee) throws SQLException {
    String sql;
    sql = "INSERT INTO consignee (consigneename, salename, phone, address, postalcode, mail) VALUES (?, ?, ?, ?, ?, ?)";
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = ds.getConnection();
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, consignee.getConsigneename());
      pstmt.setString(2, consignee.getSalename());
      pstmt.setString(3, consignee.getPhone());
      pstmt.setString(4, consignee.getAddress());
      pstmt.setString(5, consignee.getPostalcode());
      pstmt.setString(6, consignee.getMail());
      pstmt.executeUpdate();
      pstmt.close();
      conn.commit();
    } catch (SQLException sqle) {
      close(rs);
      close(pstmt);
      rollback(conn);
      sqle.printStackTrace();
      throw sqle;
    } finally {
    	close(conn);
    }
  }

  public void update(Consignee consignee) throws SQLException {
  	Connection conn = null;
  	PreparedStatement pstmt = null;
    try {
      conn = ds.getConnection();
      String sql = "UPDATE consignee SET phone=?, address=?, postalcode=?, mail=? WHERE consigneename=? AND salename=?";
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, consignee.getPhone());
      pstmt.setString(2, consignee.getAddress());
      pstmt.setString(3, consignee.getPostalcode());
      pstmt.setString(4, consignee.getMail());
      pstmt.setString(5, consignee.getConsigneename());
      pstmt.setString(6, consignee.getSalename());
      pstmt.executeUpdate();
      close(pstmt);
      conn.commit();
    } catch (SQLException e) {
      close(pstmt);
      rollback(conn);
      e.printStackTrace();
    } finally {
    	close(conn);
    }
  }

  public void delete(String consigneename, String salename) throws SQLException {
  	Connection conn = null;
  	PreparedStatement pstmt = null;
    try {
      conn = ds.getConnection();
      String sql = "DELETE FROM consignee WHERE consigneename=? AND salename=?";
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, consigneename);
      pstmt.setString(2, salename);
      pstmt.executeUpdate();
      close(pstmt);
      conn.commit();
    } catch (SQLException e) {
      close(pstmt);
      rollback(conn);
      e.printStackTrace();
    } finally {
    	close(conn);
    }
    String[] objKeys = {"Consignee", String.valueOf(consigneename), String.valueOf(salename)};
    String objKey = CacheManager.createKey(objKeys);
    DAOCacheManager.invalidate(objKey);
  }

  public Consignee retrieve(String consigneename, String salename) throws SQLException {
    String[] objKeys = {"Consignee", String.valueOf(consigneename), String.valueOf(salename)};
    String objKey = CacheManager.createKey(objKeys);
    Consignee consignee = (Consignee) DAOCacheManager.getCache(objKey);
    if (consignee != null)
      return consignee;
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = ds.getConnection();
      String sql = "SELECT * FROM consignee WHERE consigneename=? AND salename=?";
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, consigneename);
      pstmt.setString(2, salename);
      rs = pstmt.executeQuery();
      if (rs.next()) {
        consignee = new Consignee();
        populate(consignee, rs);
      }
      close(rs);
      close(pstmt);
    } catch (SQLException e) {
      close(rs);
      close(pstmt);
      rollback(conn);
      e.printStackTrace();
    } finally {
    	close(conn);
    }
    DAOCacheManager.putCache(consignee, objKey, 1);
    return consignee;
  }

  public List list() throws SQLException {
    String[] objKeys = {"Consignee", "list"};
    String objKey = CacheManager.createKey(objKeys);
    ArrayList list = (ArrayList) DAOCacheManager.getCache(objKey);
    if (list != null)
      return list;
    list = new ArrayList();
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = ds.getConnection();
      String sql = "SELECT phone, address, postalcode, mail, consigneename, salename FROM consignee";
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      while(rs.next()) {
        Consignee consignee = new Consignee();
        populate(consignee, rs);
        list.add(consignee);
      }
      close(rs);
      close(pstmt);
    } catch (SQLException e) {
      close(rs);
      close(pstmt);
      rollback(conn);
      e.printStackTrace();
    } finally {
    	close(conn);
    }
    DAOCacheManager.putCache(list, objKey, 1);
    return list;
  }

  public List list(int offset, int limit) throws SQLException {
    String[] objKeys = {"Consignee", "list", String.valueOf(offset), String.valueOf(limit)};
    String objKey = CacheManager.createKey(objKeys);
    ArrayList list = (ArrayList) DAOCacheManager.getCache(objKey);
    if (list != null)
      return list;
    list = new ArrayList();
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = ds.getConnection();
      String sql = "SELECT phone, address, postalcode, mail, consigneename, salename FROM consignee";
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      if(offset > 0) rs.absolute(offset);
      int recCount = 0;
      while((recCount++ < limit) && rs.next()) {
        Consignee consignee = new Consignee();
        populate(consignee, rs);
        list.add(consignee);
      }
      close(rs);
      close(pstmt);
    } catch (SQLException e) {
      close(rs);
      close(pstmt);
      rollback(conn);
      e.printStackTrace();
    } finally {
    	close(conn);
    }
    DAOCacheManager.putCache(list, objKey, 1);
    return list;
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?