sql的简单增删改查

来源:互联网 发布:阿里云cdn combo 编辑:程序博客网 时间:2024/06/06 02:44
package DAO;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import entity.product;public class zengshangaicha extends jidao{public void add(product pp){StringBuilder sql=new StringBuilder("insert into product(pid,pname,pprice,plei,pdate) ");sql.append("values(?,?,?,?,?)");String execSql = sql.toString();java.sql.Date bornDate = new java.sql.Date(pp.getDate().getTime());Object[] list={pp.getId(),pp.getName(),pp.getPrice(),pp.getPlei(),bornDate};super.execUpdate(execSql, list);}public List<product> getall(){String sql="select * from product";ResultSet rs=super.execQuery(sql, null);List<product> list=new ArrayList<product>();try {while(rs.next()){product pp1=new product();pp1.setId(rs.getInt("pid"));pp1.setName(rs.getString("pname"));pp1.setPrice(rs.getFloat("pprice"));pp1.setPlei(rs.getString("plei"));pp1.setDate(rs.getDate("pdate"));list.add(pp1);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}public void del(int id){String sql="delete from product where pid=? ";super.execUpdate(sql, new Object[]{id});}public List<product> findbyid(int id){String sql="select * from product where pid=? ";ResultSet rs=super.execQuery(sql, new Object[]{id});List<product> list=new ArrayList<product>();try {if(rs.next()){product pp1=new product();pp1.setId(rs.getInt("pid"));pp1.setName(rs.getString("pname"));pp1.setPrice(rs.getFloat("pprice"));pp1.setPlei(rs.getString("plei"));pp1.setDate(rs.getDate("pdate"));list.add(pp1);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}public void update(product pp2){String sql="update product set pname=?,pprice=?,plei=?,pdate=? where pid=? ";java.sql.Date date1 = new java.sql.Date(pp2.getDate().getTime());Object[] oo={pp2.getName(),pp2.getPrice(),pp2.getPlei(),pp2.getDate(),pp2.getId()};super.execUpdate(sql, oo);}}

0 0
原创粉丝点击