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

📄 productmanagerdaojdbc.java

📁 朱冰-springmvc
💻 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;
        }

    }

}

⌨️ 快捷键说明

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