快速业务通道

利用JBuilder 2005开发Spring实例 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-23
oducts (id, description, price) values(1, ''Lamp'', 5.78); INSERT INTO products (id, description, price) values(2, ''Table'', 75.29); INSERT INTO products (id, description, price) values(3, ''Chair'', 22.81);

这样就会在JBuilder_Home/bin目录下创建一个目录db,存放了数据库test的数据

22. 创建JDBC DAO (Data Access Object)实现

springapp/src/db/ProductManagerDao.java

package db; import bus.Product; import java.util.List; public interface ProductManagerDao { public List getProductList(); public void increasePrice(Product prod, int pct); }

springapp/src/db/ProductManagerDaoJdbc.java

package db; import bus.Product; import java.util.List; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.jdbc.object.MappingSqlQuery; import org.springframework.jdbc.object.SqlUpdate; import org.springframework.jdbc.core.SqlParameter; public class ProductManagerDaoJdbc implements ProductManagerDao {   /** Logger for this class and subclasses */   protected final Log logger = LogFactory.getLog(getClass());   private DataSource ds;   public List getProductList() {    logger.info("Getting products!");    ProductQuery pq = new ProductQuery(ds);    return pq.execute();   }   public void increasePrice(Product prod, int pct) {    logger.info("Increasing price by " + pct + "%");    SqlUpdate su = new SqlUpdate(ds, "update products set price = price * (100 + ?) / 100 where id = ?");    su.declareParameter(new SqlParameter("increase", Types.INTEGER));    su.declareParameter(new SqlParameter("ID", Types.INTEGER));    su.compile();    Object[] oa = new Object[2];    oa[0] = new Integer(pct);    oa[1] = new Integer(prod.getId());    int count = su.update(oa);    logger.info("Rows affected: " + count);   }   public void setDataSource(DataSource ds) {    this.ds = ds;   }   class ProductQuery extends MappingSqlQuery {    ProductQuery(DataSource ds) {     super(ds, "SELECT id, description, price from products");     compile();    }    protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {     Product prod = new Product();     prod.setId(rs.getInt("id"));     prod.setDescription(rs.getString("description"));     prod.setPrice(new Double(rs.getDouble("price")));     return prod;    }   } }

利用JBuilder 2005开发Spring实例(7)

时间:2010-12-04

springapp/src/bus/Product.java

package bus; import java.io.Serializable; public class Product implements Serializable { private int id; private String description; private Double price; public void setId(int i) { id = i; } public int getId() { return id; } public void setDescription(String s) { description = s; } public String getDescription() { return description; } public void setPr

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号