📄 productbeanbo.java
字号:
package com.back.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import com.my.model.ConnDB;
public class ProductBeanBO {
private ResultSet rs=null;
private Connection ct=null;
private PreparedStatement ps=null;
private Statement sm=null;
private int pageSize = 3;
private int rowCount = 0;
private int pageCount = 0;
public int checkGoods(String goodsName,String goodsIntro,float goodsPrice,String photo)
{
int c=0;
if(goodsName==null)
{
c=1;//商品名称为空
}else if(goodsIntro==null)
{
c=2;//商品简介为空
}else if(goodsPrice==0)
{
c=3;//商品价格为空
}else if(photo==null)
{
c=4;//商品图片为空
}else
{
c=5;//可以添加
}
return c;
}
public int addGoods(String goodsName,String goodsIntro,float goodsPrice,int goodsNum,String type,String publisher,String photo,int skimTimes)
{
int b=0;
int m=this.checkGoods(goodsName, goodsIntro, goodsPrice, photo);
if(m==1)
{
b=2;//商品名称为空
}else if(m==2)
{
b=3;//商品简介为空
}else if(m==3)
{
b=4;//商品价格为空
}else if(m==4)
{
b=5;//商品图片为空
}else if(m==5)
{
try
{
ct=new ConnDB().getConn();
sm=ct.createStatement();
int l=sm.executeUpdate("insert into goods(goodsName,goodsIntro,goodsPrice,goodsNum,type,publisher,photo,skimTimes) values ('"+goodsName+"','"+goodsIntro+"','"+goodsPrice+"','"+goodsNum+"','"+type+"','"+publisher+"','"+photo+"','"+skimTimes+"')");
if(l==1)
{
b=1;//商品添加成功
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
this.close();
}
}
return b;
}
public boolean deleteGoods(String id)
{
boolean b=false;
try
{
ct=new ConnDB().getConn();
sm=ct.createStatement();
int a=sm.executeUpdate("delete from goods where goodsId='"+id+"'");
if(a==1)
{
b=true;
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
this.close();
}
return b;
}
public ArrayList<ProductBean> getProductBean()
{
ArrayList<ProductBean> al=new ArrayList<ProductBean>();
ProductBean pb=new ProductBean();
try
{
ct=new ConnDB().getConn();
ps=ct.prepareStatement("select * from goods");
rs=ps.executeQuery();
while(rs.next())
{
pb.setGoodsId(rs.getInt(1));
pb.setGoodsName(rs.getString(2));
pb.setGoodsIntro(rs.getString(3));
pb.setGoodsPrice(rs.getFloat(4));
pb.setGoodsNum(rs.getInt(5));
pb.setPublisher(rs.getString(6));
pb.setPhoto(rs.getString(7));
pb.setType(rs.getString(8));
pb.setSkimTimes(rs.getInt(9));
al.add(pb);
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
this.close();
}
return al;
}
public int getPageCount() {
try {
ct = new ConnDB().getConn();
sm=ct.createStatement();
rs = sm.executeQuery("select count(*) from goods");
if (rs.next()) {
rowCount = rs.getInt(1);
//System.out.println(rowCount);
}
if (rowCount % pageSize == 0) {
pageCount = rowCount / pageSize;
} else {
pageCount = rowCount / pageSize + 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return pageCount;
}
public ArrayList<ProductBean> getGoodsByPage(int pageSize,int pageNow)
{
ArrayList<ProductBean> al=new ArrayList<ProductBean>();
try
{
int a=this.getPageCount();
if(pageNow<1|pageNow>a)
{
al=null;
}else{
ct=new ConnDB().getConn();
ps=ct.prepareStatement("select * from goods limit "+(pageNow-1)*pageSize+","+pageSize+"");
rs=ps.executeQuery();
while(rs.next())
{
ProductBean pb=new ProductBean();
pb.setGoodsId(rs.getInt(1));
pb.setGoodsName(rs.getString(2));
pb.setGoodsIntro(rs.getString(3));
pb.setGoodsPrice(rs.getFloat(4));
pb.setGoodsNum(rs.getInt(5));
pb.setPublisher(rs.getString(6));
pb.setPhoto(rs.getString(7));
pb.setType(rs.getString(8));
pb.setSkimTimes(rs.getInt(9));
al.add(pb);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}finally
{
this.close();
}
return al;
}
public ProductBean getProductBean(String goodsName)
{
ProductBean pb=new ProductBean();
try
{
ct=new ConnDB().getConn();
ps=ct.prepareStatement("select * from goods where goodsName ='"+goodsName+"'");
rs=ps.executeQuery();
if(rs.next())
{
pb.setGoodsId(rs.getInt(1));
pb.setGoodsName(rs.getString(2));
pb.setGoodsIntro(rs.getString(3));
pb.setGoodsPrice(rs.getFloat(4));
pb.setGoodsNum(rs.getInt(5));
pb.setPublisher(rs.getString(6));
pb.setPhoto(rs.getString(7));
pb.setType(rs.getString(8));
pb.setSkimTimes(rs.getInt(9));
}else
{
pb=null;
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
this.close();
}
return pb;
}
public boolean updateGoods(int id,String goodsName,String goodsIntro,float goodsPrice,int goodsNum,String publisher,String photo,String type,int skimTimes)
{
boolean b=false;
try{
ct=new ConnDB().getConn();
sm=ct.createStatement();
int l=sm.executeUpdate("update goods set goodsName='"+goodsName+"',goodsIntro='"+goodsIntro+"',goodsPrice='"+goodsPrice+"',goodsNum='"+goodsNum+"',publisher='"+publisher+"',photo='"+photo+"',type='"+type+"',skimTimes='"+skimTimes+"' where goodsId='"+id+"'");
//System.out.println(l);
if(l==1)
{
b=true;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}finally{
this.close();
}
return b;
}
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
if (ct != null) {
ct.close();
ct = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -