⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientdao.java

📁 本例采用sql server数据库。基于JBuilder的tomcat上运行。访问数据库基于struts的datasources.里面有详细的操作和设置说明。
💻 JAVA
字号:
//---------------------------------------------------------
// Application: Gsm of Application
// Author     : esingle
// File       : ClientDAO.java
//
// Copyright 2004 landsoft corp
// Generated at Wed Mar 10 15:35:57 CST 2004
// created by 曹广鑫
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------

package com.landsoft.gsm.dao;

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

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

public class ClientDAO extends DAO {

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

  public void insert(Client client) throws SQLException {
    String sql;
    sql = "INSERT INTO client (mobile, name, sfzh) VALUES (?, ?, ?)";
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      conn = ds.getConnection();
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, client.getMobile());
      pstmt.setString(2, client.getName());
      pstmt.setString(3, client.getSfzh());
      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(Client client) throws SQLException {
  	Connection conn = null;
  	PreparedStatement pstmt = null;
    try {
      conn = ds.getConnection();
      String sql = "UPDATE client SET name=?, sfzh=? WHERE mobile=?";
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, client.getName());
      pstmt.setString(2, client.getSfzh());
      pstmt.setString(3, client.getMobile());
      pstmt.executeUpdate();
      close(pstmt);
      conn.commit();
    } catch (SQLException e) {
      close(pstmt);
      rollback(conn);
      e.printStackTrace();
    } finally {
    	close(conn);
    }
  }

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

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

  public List list() throws SQLException {
    String[] objKeys = {"Client", "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 name, sfzh, mobile FROM client";
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      while(rs.next()) {
        Client client = new Client();
        populate(client, rs);
        list.add(client);
      }
      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 = {"Client", "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 name, sfzh, mobile FROM client";
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      if(offset > 0) rs.absolute(offset);
      int recCount = 0;
      while((recCount++ < limit) && rs.next()) {
        Client client = new Client();
        populate(client, rs);
        list.add(client);
      }
      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 newQuery(int offset, int limit, String mobile, String name, String sfzh) throws SQLException {
    String[] objKeys = {"Client", "newQuery", String.valueOf(offset), String.valueOf(limit), String.valueOf(mobile), String.valueOf(name), String.valueOf(sfzh)};
    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 name, sfzh, mobile client";
      sql += " WHERE mobile =? AND name =? AND sfzh =?";
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, mobile);
      pstmt.setString(2, name);
      pstmt.setString(3, sfzh);
      rs = pstmt.executeQuery();
      if(offset > 0) rs.absolute(offset);
      int recCount = 0;
      while((recCount++ < limit) && rs.next()) {
        Client client = new Client();
        populate(client, rs);
        list.add(client);
      }
      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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -