DAO就是访问底层的数据库
DTO就是数据传输对象
我现在将例子贴上,不过你可要给分呀!

解决方案 »

  1.   

    ----------------------------------DTO-------------------------------------import com.lanxin.dto.BaseDTO;public class CareerDTO extends BaseDTO{ private String id;
    private String career; public CareerDTO() {
        } public void setId(String _id){
    this.id=_id;
    } public String getId(){
    return id;
    } public void setCareer(String _career){
    this.career=_career;
    }
    public String getCareer(){
    return career;
    }
    }
      

  2.   

    -----------------------------------DAO--------------------------package com.sun.j2ee.blueprints.catalog.dao;import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    import java.util.*;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    import javax.naming.NamingException;import com.sun.j2ee.blueprints.catalog.util.JNDINames;
    import com.sun.j2ee.blueprints.catalog.model.Page;
    import com.sun.j2ee.blueprints.catalog.model.Category;
    import com.sun.j2ee.blueprints.catalog.model.Product;
    import com.sun.j2ee.blueprints.catalog.model.Item;
    import com.sun.j2ee.blueprints.catalog.util.DatabaseNames;
    import com.sun.j2ee.blueprints.catalog.exceptions.CatalogDAOSysException;import com.sun.j2ee.blueprints.util.tracer.Debug;/**
     * This class implements CatalogDAO for oracle, sybase and cloudscape DBs.
     * This class encapsulates all the SQL calls made by Catalog EJB.
     * This layer maps the relational data stored in the database to
     * the objects needed by Catalog EJB.
    */
    public class CatalogDAOImpl implements CatalogDAO {    // Helper methods    protected static DataSource getDataSource()
            throws CatalogDAOSysException {
            try {
                InitialContext ic = new InitialContext();
                return (DataSource) ic.lookup(JNDINames.CATALOG_DATASOURCE);
            }
            catch (NamingException ne) {
                throw new CatalogDAOSysException("NamingException while looking "
                                                 + "up DB context : "
                                                 + ne.getMessage());
            }
        }    // Business methods    public Category getCategory(String categoryID, Locale l)
            throws CatalogDAOSysException {        Connection c = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            Category ret = null;        try {
                c = getDataSource().getConnection();            ps = c.prepareStatement("select a.catid, name, descn "
                                        + "from (category a join "
                                        + "category_details b on "
                                        + "a.catid=b.catid) "
                                        + "where locale = ? "
                                        + "and a.catid = ?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
                ps.setString(1, l.toString());
                ps.setString(2, categoryID);
                rs = ps.executeQuery();
                if (rs.first()) {
                    ret = new Category(rs.getString(1).trim(),
                                       rs.getString(2),
                                       rs.getString(3));
                }
                rs.close();
                ps.close();            c.close();
                return ret;
            }
            catch (SQLException se) {
                throw new CatalogDAOSysException("SQLException: "
                                                 + se.getMessage());
            }
         }    public Page getCategories(int start, int count, Locale l)
            throws CatalogDAOSysException {        Connection c = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            Page ret = null;        try {
                c = getDataSource().getConnection();            // Count.
                ps = c.prepareStatement("select COUNT(*) "
                                        + "from (category a join "
                                        + "category_details b on "
                                        + "a.catid=b.catid) "
                                        + "where locale = ?",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
                ps.setString(1, l.toString());
                rs = ps.executeQuery();
                rs.first();
                int total = rs.getInt(1);
                rs.close();
                ps.close();            // Select.
                ps = c.prepareStatement("select a.catid, name, descn "
                                        + "from (category a join "
                                        + "category_details b on "
                                        + "a.catid=b.catid) "
                                        + "where locale = ? "
                                        + "order by name",
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
                ps.setString(1, l.toString());            rs = ps.executeQuery();
                if (start >= 0 && start < total) {
                    List items = new ArrayList();
                    rs.absolute(start+1);
                    do {
                        items.add(new Category(rs.getString(1).trim(),
                                               rs.getString(2),
                                               rs.getString(3)));
                    } while (rs.next() && (--count > 0));
                    ret = new Page(items, start, total);
                }
                else {
                    ret = Page.EMPTY_PAGE;
                }            rs.close();
                ps.close();            c.close();
                return ret;
            }
            catch (SQLException se) {
                throw new CatalogDAOSysException("SQLException: "
                                                 + se.getMessage());
            }
        }
    }
      

  3.   

    一个很好的教程
    http://expert.csdn.net/Expert/TopicView1.asp?id=2749637
    如果楼主下载不了,
    再发消息告诉我你的email。
    我可以发个给你。