📄 productdao.java
字号:
package com.runwit.ebookstore.services.dao;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.runwit.common.db.DbDAO;
import com.runwit.common.util.PageResult;
import com.runwit.common.util.PageUtil;
import com.runwit.ebookstore.model.CategoryModel;
import com.runwit.ebookstore.model.ProductModel;
import com.runwit.ebookstore.services.IProductServices;
public class ProductDAO extends DbDAO implements IProductServices {
public boolean createProduct(ProductModel model) {
// productid, 自增长
String sql = "insert into product(category_id, isbn, name, price, "
+ "publisher, picture, editionNumber, authors, content, "
+ "discount, listinfo, publisherdate, createtime, status) "
+ "values(?, ?, ?, ?,?, ?, ?, ?, ?,?, ?, ?, ?, ?)";
Object[] paramObj = { model.getIsbn(), model.getName(),
model.getPrice(), model.getPublisher(), model.getPicByte(),
model.getEditionNumber(), model.getAuthors(),
model.getContent(), model.getDiscount(), model.getListinfo(),
model.getPublisherDate(), model.getCreateTime(),
model.getStatus() };
int[] paramType = { STRING_TYPE, STRING_TYPE, BIGDECIMAL_TYPE,
STRING_TYPE, BYTEARRAY_TYPE, INT_TYPE };
try {
int affectCount = updateBySql(sql, paramObj, paramType);
if(affectCount == 1)
model.setProductId(getMaxID("product_id", "product"));
return affectCount == 1;
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
}
return false;
}
public List listProduct(PageUtil pageUtil, PageResult pageResult) {
int fromIdx = pageUtil.getFromIndex();
int toIdx = pageUtil.getToIndex();
try
{
openConnection();
openStatement();
String sql1 = "select top "+toIdx+" * from product where product_id not in (select top "+ fromIdx+"* from product)";
rs = stmt.executeQuery(sql1);
pageResult.getResult().clear();
while(rs.next()) {
ProductModel m = new ProductModel();
m.setAuthors(rs.getString("authors"));
m.setCategoryId(rs.getInt("category_id"));
m.setContent(rs.getString("content"));
m.setCreateTime(rs.getLong("createTime"));
m.setDiscount(rs.getInt("discount"));
m.setEditionNumber(rs.getInt("editionNumber"));
m.setIsbn(rs.getString("isbn"));
m.setListinfo(rs.getString("listinfo"));
m.setName(rs.getString("name"));
m.setPicByte(rs.getBytes("picture"));
m.setPrice(new BigDecimal(rs.getDouble("price")));
m.setProductId(rs.getLong("product_id"));
m.setPublisher(rs.getString("publisher"));
m.setPublisherDate(rs.getLong("publisherdate"));
m.setStatus(rs.getInt("status"));
pageResult.getResult().add(m);
}
closeResultSet();
if(pageResult.getTotalCount() < 0) {
String sql2 = "select count(*) from product";
rs = stmt.executeQuery(sql2);
if(rs.next()) {
pageResult.setTotalCount(rs.getInt(1));
}
}
}catch(SQLException ex) {
ex.printStackTrace();
}finally{
closeResultSet();
closeStatement();
closeConnection();
}
return null;
}
public List searchProduct(String isbn) {
// TODO Auto-generated method stub
return null;
}
public boolean updateBaseInfo(ProductModel model) {
// TODO Auto-generated method stub
return false;
}
public boolean updatePicture(long productId, byte[] pics) {
String sql = "update product set picture=? where product_id=?";
Object[] paramObj = { new Long(productId), pics };
int[] paramType = { LONG_TYPE, BYTEARRAY_TYPE};
try {
int affectCount = updateBySql(sql, paramObj, paramType);
return affectCount == 1;
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
}
return false;
}
public Object mapRowToModel(ResultSet rs) throws SQLException{
ProductModel m = new ProductModel();
m.setAuthors(rs.getString("authors"));
m.setCategoryId(rs.getInt("category_id"));
m.setContent(rs.getString("content"));
m.setCreateTime(rs.getLong("createTime"));
m.setDiscount(rs.getInt("discount"));
m.setEditionNumber(rs.getInt("editionNumber"));
m.setIsbn(rs.getString("isbn"));
m.setListinfo(rs.getString("listinfo"));
m.setName(rs.getString("name"));
m.setPicByte(rs.getBytes("picture"));
m.setPrice(new BigDecimal(rs.getDouble("price")));
m.setProductId(rs.getLong("product_id"));
m.setPublisher(rs.getString("publisher"));
m.setPublisherDate(rs.getLong("publisherdate"));
m.setStatus(rs.getInt("status"));
return m;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -