📄 goodsdaoimpl.java
字号:
package com.eshop.daoImpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import com.eshop.database.dbconn;
import com.eshop.dao.GoodsDao;
import com.eshop.dto.Goods;
/*
* Vector 元素为goods对象
* */
public class GoodsDaoImpl implements GoodsDao
{
//输出所有
public Vector selectAll() throws SQLException{
Vector list = new Vector();
dbconn conn = new dbconn();
String sql = "select * from goods ";
ResultSet rs = null;
rs = conn.executeQuery(sql);
while(rs.next())
{
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setBrand(rs.getString("brand"));
goods.setIntime(rs.getString("intime"));
goods.setIntroduc(rs.getString("introduce"));
goods.setNowprice(rs.getDouble("nowprice"));
goods.setPicture(rs.getString("picture"));
goods.setPrice(rs.getDouble("price"));
goods.setType(rs.getString("type"));
goods.setSaletol(rs.getInt("saletol"));
list.add(goods);
}
return list;
}
public Goods FindById(int id) throws SQLException{
Goods goods = new Goods();
dbconn conn = new dbconn();
String sql = "select * from goods where id = "+id+"";
ResultSet rs = null;
rs = conn.executeQuery(sql);
while(rs.next())
{
goods.setId(rs.getInt("id"));
goods.setBrand(rs.getString("brand"));
goods.setIntime(rs.getString("intime"));
goods.setIntroduc(rs.getString("introduce"));
goods.setNowprice(rs.getDouble("nowprice"));
goods.setPicture(rs.getString("picture"));
goods.setPrice(rs.getDouble("price"));
goods.setType(rs.getString("type"));
goods.setSaletol(rs.getInt("saletol"));
}
return goods;
}
//按品牌查询
public Vector FindByBrand(String brand) throws SQLException {
Vector list = new Vector();
dbconn conn = new dbconn();
String sql = "select * from goods where brand like '%"+brand+"%'";
ResultSet rs = null;
rs = conn.executeQuery(sql);
while(rs.next())
{
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setBrand(rs.getString("brand"));
goods.setIntime(rs.getString("intime"));
goods.setIntroduc(rs.getString("introduce"));
goods.setNowprice(rs.getDouble("nowprice"));
goods.setPicture(rs.getString("picture"));
goods.setPrice(rs.getDouble("price"));
goods.setType(rs.getString("type"));
goods.setSaletol(rs.getInt("saletol"));
list.add(goods);
}
return list;
}
//按价格查询
public Vector FindByPrice(double from, double to) throws SQLException {
Vector list = new Vector();
dbconn conn = new dbconn();
String sql = "select * from goods where nowprice>="+from+" and nowpricw <="+to;
ResultSet rs = null;
rs = conn.executeQuery(sql);
while(rs.next())
{
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setBrand(rs.getString("brand"));
goods.setIntime(rs.getString("intime"));
goods.setIntroduc(rs.getString("introduce"));
goods.setNowprice(rs.getDouble("nowprice"));
goods.setPicture(rs.getString("picture"));
goods.setPrice(rs.getDouble("price"));
goods.setType(rs.getString("type"));
goods.setSaletol(rs.getInt("saletol"));
list.add(goods);
}
return list;
}
//按型号查询
public Vector FindByType(String type) throws SQLException {
Vector list = new Vector();
dbconn conn = new dbconn();
String sql = "select * from goods where brand like '%"+type+"%'";
ResultSet rs = null;
rs = conn.executeQuery(sql);
while(rs.next())
{
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setBrand(rs.getString("brand"));
goods.setIntime(rs.getString("intime"));
goods.setIntroduc(rs.getString("introduce"));
goods.setNowprice(rs.getDouble("nowprice"));
goods.setPicture(rs.getString("picture"));
goods.setPrice(rs.getDouble("price"));
goods.setType(rs.getString("type"));
goods.setSaletol(rs.getInt("saletol"));
list.add(goods);
}
return list;
}
public int delete(int id) throws SQLException {
String sql = "delete from goods where id = "+id;
dbconn conn = new dbconn();
int flag =0;
flag = conn.excuteUpdate(sql);
return flag;
}
public int insert(Goods g) throws SQLException {
String sql = "insert into goods (brand,type,introduce,price,nowprice,picture,intime)" +
" values ('"+g.getBrand()+"','"+g.getType()+"','"+g.getIntroduc()+"','"+
g.getPrice()+"','"+g.getNowprice()+"','"+g.getPicture()+"','"+g.getIntime()+"')";
dbconn conn = new dbconn();
int flag = 0;
flag = conn.excuteUpdate(sql);
return flag;
}
public int update(Goods g) throws SQLException {
String sql = "update goods set brand = '"+g.getBrand()+"', type = '"+g.getType()+
"',introduce = '"+g.getIntroduc()+"',price ='"+g.getPrice()+"',nowprice ='"+
g.getNowprice()+"',picture='"+g.getPicture()+"',intime ='"+g.getIntime()
+"' where id ="+g.getId();
dbconn conn = new dbconn();
int flag = 0;
flag = conn.excuteUpdate(sql);
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -