哪个兄弟,能给我发一个DAO和TO的例子。

wzl_chenling 2003-09-04 02:55:24
wangzhilong@iholley.com
...全文
90 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Leemaasn 2004-03-26
  • 打赏
  • 举报
回复
一个很好的教程
http://expert.csdn.net/Expert/TopicView1.asp?id=2749637
如果楼主下载不了,
再发消息告诉我你的email。
我可以发个给你。
xqi8 2004-03-26
  • 打赏
  • 举报
回复
up
llpei 2003-09-04
  • 打赏
  • 举报
回复
-----------------------------------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());
}
}
}
llpei 2003-09-04
  • 打赏
  • 举报
回复
----------------------------------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;
}
}
llpei 2003-09-04
  • 打赏
  • 举报
回复
DAO就是访问底层的数据库
DTO就是数据传输对象
我现在将例子贴上,不过你可要给分呀!

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧