WEB开发网
开发学院图形图像Flash 跟我StepByStep学FLEX教程------Demo13之Fl... 阅读

跟我StepByStep学FLEX教程------Demo13之Flex访问数据库

 2009-09-09 00:00:00 来源:WEB开发网   
核心提示: ProductDAO.java(业务层接口)package com.samples.spring.store;import java.util.Collection;import org.springframework.dao.DataAccessException;public interfa

ProductDAO.java(业务层接口)

package com.samples.spring.store;

import java.util.Collection;
import org.springframework.dao.DataAccessException;

public interface ProductDAO {

 public Collection findAll() throws DataAccessException ;
 
 public void createProduct(Product product) throws DataAccessException ;
 
 public void updateProduct(Product product) throws DataAccessException ;
 
 public void deleteProduct(Product product) throws DataAccessException ;
 
}

SimpleProductDAO.java(业务层实现)

package com.samples.spring.store;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class SimpleProductDAO extends JdbcDaoSupport implements ProductDAO {
 
 public Collection findAll() throws DataAccessException {
  String sql = "SELECT * FROM product";
  
  RowMapper mapper = new RowMapper() {
   
   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    Product product = new Product();
    product.setProductId(rs.getLong("product_id"));
    product.setName(rs.getString("name"));
    product.setDescription(rs.getString("description"));
    product.setCategory(rs.getString("category"));
    product.setImage(rs.getString("image"));
    product.setPrice(rs.getDouble("price"));
    product.setQtyInStock(rs.getInt("qty_in_stock"));
    return product;
   }
   
  };
  
  JdbcTemplate template = new JdbcTemplate(this.getDataSource());
  System.out.println("sql" + sql + "mapper" + mapper.toString());
  return template.query(sql, mapper);
 }

 public void createProduct(Product product) throws DataAccessException {
  String sql = "INSERT INTO product (name, description, category, image, price, qty_in_stock) VALUES (:name, :description, :category, :image, :price, :qtyInStock)";
  NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(this.getDataSource());
  SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(product);
  template.update(sql, namedParameters);
  product.setProductId(getJdbcTemplate().queryForInt("call identity()"));
 }

 public void updateProduct(Product product) throws DataAccessException {
  String sql = "UPDATE product SET name=:name,description=:description,category=:category,image=:image,price=:price,qty_in_stock=:qtyInStock WHERE product_id=:productId";
  NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(this.getDataSource());
  SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(product);
  template.update(sql, namedParameters);
 }

 public void deleteProduct(Product product) throws DataAccessException {
  String sql = "DELETE from product WHERE product_id=:productId";
  NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(this.getDataSource());
  SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(product);
  template.update(sql, namedParameters);
 }

}

上一页  1 2 3 4 5  下一页

Tags:StepByStep FLEX 教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接