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

📄 catalogdaojdbc.java~3~

📁 用JBuilder+Jboos+mysql实现的EJB项目
💻 JAVA~3~
字号:
package com.jdon.estore.catalog.dao;

import com.jdon.estore.model.Category;
import com.jdon.controller.model.PageIterator;
import com.jdon.estore.model.Product;
import com.jdon.estore.model.Item;

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

import com.jdon.servicelocator.ejb.ServiceLocator;
import com.jdon.servicelocator.ServiceLocatorException;

import com.jdon.controller.model.PageIterator;
import com.jdon.util.DbUtil;
import com.jdon.estore.catalog.JNDINames;

import org.apache.log4j.Logger;

public class CatalogDAOJDBC implements CatalogDAO {
  private final static Logger logger = Logger.getLogger(CatalogDAOJDBC.class);
  private DataSource dataSource = null;

  private final static String GET_CATEGORY =
      "select * from category_details where catId = ?";

  private final static String GET_CATEGORIES =
      "select category.catId from category";

  private final static String GET_CATEGORIES_ALLCOUNT =
      "select count(1) from category";

  protected  DataSource getDataSource() throws Exception {
    try {
      if (dataSource == null){
        ServiceLocator sl = new ServiceLocator();
        dataSource = (DataSource) sl.getDataSource(JNDINames.CATALOG_DATASOURCE);
      }
      return dataSource;
    } catch (ServiceLocatorException slx) {
      logger.error(slx);
      throw new Exception(slx);
    }
  }

  public Category getCategory(String catId) throws Exception {
    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Category ret = null;
    try {
      c = getDataSource().getConnection();
      ps = c.prepareStatement(GET_CATEGORY,
                              ResultSet.TYPE_SCROLL_INSENSITIVE,
                              ResultSet.CONCUR_READ_ONLY);
      ps.setString(1, catId);
      rs = ps.executeQuery();
      if (rs.first()) {
        ret = new Category(catId, rs.getString(1), rs.getString(2));
      }
    } catch (SQLException se) {
      throw new Exception("SQLException: " + se.getMessage());
    } finally {
      if (rs != null)
        rs.close();
      if (ps != null)
        ps.close();
      if (c != null)
        c.close();
    }
    return ret;
  }

  public int getCategoryAllCount() throws Exception {
    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int ret = 0;
    try {
      c = getDataSource().getConnection();
      ps = c.prepareStatement(GET_CATEGORIES_ALLCOUNT,
                              ResultSet.TYPE_SCROLL_INSENSITIVE,
                              ResultSet.CONCUR_READ_ONLY);
      rs = ps.executeQuery();
      if (rs.first()) {
        ret = rs.getInt(1);
      }
    } catch (SQLException se) {
      throw new Exception("SQLException: " + se.getMessage());
    } finally {
      if (rs != null)
        rs.close();
      if (ps != null)
        ps.close();
      if (c != null)
        c.close();
    }
    return ret;

  }

  public PageIterator getCategories(int start, int count) throws Exception {
    logger.debug("--> getCategores from start=" + start + " size=" + count);
    Connection c = null;
    Statement stmt = null;
    ResultSet rs = null;
    PageIterator pageIterator = null;
    boolean hasNext = false;
    try {
      c = getDataSource().getConnection();
      stmt = c.createStatement();

      DbUtil.testConnection(c);
      if (DbUtil.supportsMaxRows)
        stmt.setMaxRows(count);
      rs = stmt.executeQuery(GET_CATEGORIES);
      if (DbUtil.supportsFetchSize)
        rs.setFetchSize(count);
      if (start >= 0 && rs.absolute(start + 1)) {
        List items = new ArrayList();
        do {
          items.add(rs.getString(1));
        }
        while ( (hasNext = rs.next()) && (--count > 0));
        String[] itemsA = (String[]) items.toArray(new String[0]);
        pageIterator = new PageIterator(itemsA, start, hasNext);
      }else
         pageIterator = new PageIterator(PageIterator., start, hasNext);
      logger.debug("--> getCategores succefully ..");
    } catch (SQLException se) {
      logger.error(se);
      throw new Exception("SQLException: " + se.getMessage());
    } finally {
      if (rs != null)
        rs.close();
      if (stmt != null)
        stmt.close();
      if (c != null)
        c.close();
    }
    return pageIterator;
  }

  public Product getProduct(String productId) throws Exception {
    /**@todo Implement this com.jdon.estore.catalog.dao.CatalogDAO method*/
    throw new java.lang.UnsupportedOperationException(
        "Method getProduct() not yet implemented.");
  }

  public PageIterator getProducts(String catId, int start, int count) throws
      Exception {
    /**@todo Implement this com.jdon.estore.catalog.dao.CatalogDAO method*/
    throw new java.lang.UnsupportedOperationException(
        "Method getProducts() not yet implemented.");
  }

  public Item getItem(String itemId) throws Exception {
    /**@todo Implement this com.jdon.estore.catalog.dao.CatalogDAO method*/
    throw new java.lang.UnsupportedOperationException(
        "Method getItem() not yet implemented.");
  }

  public PageIterator getItems(String productId, int start, int size) throws
      Exception {
    /**@todo Implement this com.jdon.estore.catalog.dao.CatalogDAO method*/
    throw new java.lang.UnsupportedOperationException(
        "Method getItems() not yet implemented.");
  }

  public PageIterator searchItems(String query, int start, int size) throws
      Exception {
    /**@todo Implement this com.jdon.estore.catalog.dao.CatalogDAO method*/
    throw new java.lang.UnsupportedOperationException(
        "Method searchItems() not yet implemented.");
  }

}

⌨️ 快捷键说明

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