DAO就是访问底层的数据库
DTO就是数据传输对象
我现在将例子贴上,不过你可要给分呀!
DTO就是数据传输对象
我现在将例子贴上,不过你可要给分呀!
解决方案 »
- myeclipse中tomcat启动不了?
- wsdl4j的问题
- Weblogic8.1使用struts后,处理每个静态页面请求的内存开销竟然涨到了6M以上,前辈请指教如何调优。
- 关于jasperReport+iReport自定义数据类型的问题.
- ExtJS 校验关于包含中文的“字节长”
- JBOSS,datasource jndi,数据源配置没有问题,但我在HttpServlet的init方法中不能lookup datasource
- tomcat下如何设置使访问时不需要在URL中输入8080的端口号?
- 求JAVA的C/S结构应用程序的时间控件!!!急!!!!
- httpclient做抓取,最后一步的抓取是感觉是webservice,请求报文是SOAP类型的xml
- MyBatis问题
- 查询和显示15000行数据居然用了25分钟,谁知道哪里出了问题?
- 急问那里有poi的文档?
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;
}
}
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());
}
}
}
http://expert.csdn.net/Expert/TopicView1.asp?id=2749637
如果楼主下载不了,
再发消息告诉我你的email。
我可以发个给你。