ProductDao.java

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import s2jsp.bysj.entity.Product;

import java.io.*;

 

public class ProductDao extends BaseDao {

private Connection conn = null; // 保存数据库连接

 

private PreparedStatement pstmt = null; // 用于执行SQL语句

 

private ResultSet rs = null; // 用户保存查询结果集

 

public static final int PAGE_NUM = 5;// 定义产品列表页面显示产品个数

 

/**

* 查询所有产品信息

* @return 产品信息列表

*/

public List findAllProduct() {

List list = new ArrayList();

String sql = "select * from product ";

try {

conn = this.getConn();

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

while (rs.next()) {

Product product = new Product();

product.setProductID(rs.getInt("productID"));

product.setSerialNumber(rs.getString("serialNumber"));

product.setName(rs.getString("name"));

product.setPrice(rs.getDouble("price"));

product.setBrand(rs.getString("brand"));

product.setModel(rs.getString("model"));

product.setPicture(rs.getString("picture"));

product.setDescription(rs.getString("description"));

list.add(product);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

this.closeAll(conn, pstmt, rs);

}

return list;

}

 

/**

* 根据产品ID查找相关产品信息

* @return 产品对象

*/

public Product findProductByID(String productID) {

Product product = null;

String sql = "select * from product where productID = ? ";

try {

conn = this.getConn();

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, productID);

rs = pstmt.executeQuery();

 

if (rs.next()) {

product = new Product();

product.setProductID(rs.getInt("productID"));

product.setSerialNumber(rs.getString("serialNumber"));

product.setName(rs.getString("name"));

product.setPrice(rs.getDouble("price"));

product.setBrand(rs.getString("brand"));

product.setModel(rs.getString("model"));

product.setPicture(rs.getString("picture"));

product.setDescription(rs.getString("description"));

}

} catch (Exception e) {

e.printStackTrace();

} finally {

this.closeAll(conn, pstmt, rs);

}

return product;

}

/**

* 将图片拷贝到image文件夹下

* @param dir

* @param newDir

*/

public void copyPicture(String dir){

try {

File file=new File(dir);

File file2=new File("..//..//eclipse//workspace//example//WebRoot//image//"+file.getName());

FileInputStream input=new FileInputStream(dir);

FileOutputStream out=new FileOutputStream(file2);

byte b[]=new byte[1024];

int cont=0;

while((cont=input.read(b))>0){

out.write(b, 0, cont);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 实现产品分页显示

* @param page

*            当前页数

* @return 显示产品的列表

*/

public List showProductForPage(int page) {

List list = new ArrayList();

 

int number = 0;

if (page > 1) {

number = PAGE_NUM * (page - 1);

}

String sql="select * from ( "

+ " select productid,serialnumber,name,brand,model,price,picture,description,rownumber() over(order by productid desc) as rn from product" 

+ ") as A1 where A1.rn between "+number+"  and "+(PAGE_NUM+number);

try {

conn = this.getConn();

pstmt = conn.prepareStatement(sql);

 

rs = pstmt.executeQuery();

while (rs.next()) {

Product product = new Product();

product.setProductID(rs.getInt("productID"));

product.setSerialNumber(rs.getString("serialNumber"));

product.setName(rs.getString("name"));

product.setBrand(rs.getString("brand"));

product.setModel(rs.getString("model"));

product.setPrice(rs.getDouble("price"));

product.setPicture(rs.getString("picture"));

product.setDescription(rs.getString("description"));

 

list.add(product);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

this.closeAll(conn, pstmt, rs);

}

 

return list;

}

 

/**

* 根据产品ID删除相关产品信息

* @param productID

*            产品ID

* @return 执行SQL语句影响数据库的行数

*/

public int deleteProductByID(String productID) {

String sql = "delete from product where productID = ? ";

String[] param = new String[] { productID };

int count = this.executeSQL(sql, param);

return count;

}

 

/**

* 添加产品信息

* @param product

*            封装了信息的产品对象

* @return 执行SQL语句所影响数据库的行数

*改写该方法。将图片拷贝到一个目录,然后将新目标存到数据库中。

*/

public int insertProduct(Product product) {

String sql = "insert into product(serialnumber,name,brand,model,price,picture,description) values(?,?,?,?," + product.getPrice()

+ ",?,?)";

System.out.println(product.getPrice());

System.out.println(product.getPicture());

String[] param = new String[] { product.getSerialNumber(),

product.getName(), product.getBrand(), product.getModel(),

product.getPicture(), product.getDescription() };

return this.executeSQL(sql, param);

 

}

/**

* 根据商品ID和对象参数修改商品信息

* @param product封装了数据的商品对象

* @return 返回执行SQL语句,数据库影响行数

*/

public int updateProduct(Product product) {

String sql = "update product set serialnumber=?,name=?,brand=?,model=?,price="

+ product.getPrice()

+ ",picture=?,description=? where productID="

+ product.getProductID();

String[] param = new String[] { product.getSerialNumber(),

product.getName(), product.getBrand(), product.getModel(),

product.getPicture(), product.getDescription() };

return this.executeSQL(sql, param);

}

 

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
此代码如何修改避免空指针异常package frame; import dao.ProductDAO; import dao.ProductDAOImpl; import model.Product; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; public class UpdateProductDialog extends JDialog { private boolean isOK = false; private Product product; private JTextField nameField; private JTextField priceField; private JTextArea descriptionArea; public UpdateProductDialog(Frame owner, int productId) { super(owner, "更新商品", true); setPreferredSize(new Dimension(400, 300)); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); ProductDAO productDAO = new ProductDAOImpl() { @Override public Product getProductById(int id) { return null; } }; product = productDAO.getProductById(productId); JLabel nameLabel = new JLabel("名称"); JLabel priceLabel = new JLabel("价格"); JLabel descriptionLabel = new JLabel("描述"); nameField = new JTextField(product.getName()); priceField = new JTextField(Double.toString(product.getPrice())); descriptionArea = new JTextArea(product.getDescription(), 5, 20); JButton okButton = new JButton("确定"); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = nameField.getText(); double price = Double.parseDouble(priceField.getText()); String description = descriptionArea.getText(); product.setName(name); product.setPrice(price); product.setDescription(description); isOK = true; dispose(); } }); JButton cancelButton = new JButton("取消"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); } }); JPanel panel = new JPanel(new GridLayout(4, 2)); panel.add(nameLabel); panel.add(nameField); panel.add(priceLabel); panel.add(priceField); panel.add(descriptionLabel); panel.add(new JScrollPane(descriptionArea)); panel.add(okButton); panel.add(cancelButton); setContentPane(panel); pack(); setLocationRelativeTo(owner); } public boolean isOK() { return isOK; } public Product getProduct() { return product; } }
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值