📄 dproducts.java
字号:
package com.gc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import com.gc.action.PagesBean;
import com.gc.action.Products;
import com.gc.impl.IProducts;
public class DProducts implements IProducts{
private PreparedStatement pre = null;
private Connection con = null;
private ResultSet rs = null;
public boolean modifyProducts(Products pro) {
int count = 0;
try
{
dao d = new dao();
con = d.getCon();
if(con!=null)
{
pre = con.prepareStatement("update Products set PName=?,PPrice=?,SmallCID=?,Inventory=?,PicturePath=?,PStatus=? where PID=?");
pre.setString(1,pro.getPname());
pre.setDouble(2,pro.getPprice());
pre.setInt(3,pro.getSmallcid());
pre.setInt(4,pro.getAmount());
pre.setString(5,pro.getPicturepath());
pre.setString(6,pro.getPstatus());
pre.setInt(7, pro.getPid());
count = pre.executeUpdate();
}
pre.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
if(count>0)
{
return true;
}else
{
return false;
}
}
public List<Products> selectAllProducts() {
List<Products> list = new ArrayList<Products>();
try
{
dao d = new dao();
con = d.getCon();
if(con!=null)
{
pre = con.prepareStatement("select * from Products");
rs = pre.executeQuery();
while(rs.next())
{
Products pro = new Products();
pro.setPid(rs.getInt(1));
pro.setPname(rs.getString(2));
pro.setPprice(rs.getDouble(3));
pro.setSmallcid(rs.getInt(4));
pro.setAmount(rs.getInt(5));
pro.setPstatus(rs.getString(6));
list.add(pro);
}
}
rs.close();
pre.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public List<Products> GetProductsData(PagesBean pages)
{
List<Products> list = new ArrayList<Products>();
int temp = (pages.getCurrentIndex()-1)*pages.getPageCount();
String sql = "select top "+pages.getPageCount()+" '商品编号'=PID,'商品名称'=PName,'商品价格'=PPrice,'所属类别'=(select SmallCID from SmallCategory where SmallCID=SCID),'库存'=Inventory,'状态'=PStatus from Products where PID not in(select top "+temp+" PID from Products)";
try
{
if(con!=null)
{
pre = con.prepareStatement(sql);
rs = pre.executeQuery();
while(rs.next())
{
Products p = new Products();
p.setPid(rs.getInt(1));
p.setPname(rs.getString(2));
p.setPprice(rs.getDouble(3));
p.setSmallcid(rs.getInt(4));
p.setAmount(rs.getInt(5));
p.setPstatus(rs.getString(6).trim());
list.add(p);
}
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {
rs.close();
pre.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
//计算有多少条数据
public int GetDataCount()
{
int count = 0;
try
{
dao d = new dao();
con = d.getCon();
pre = con.prepareStatement("select count(PID) from Products");
rs = pre.executeQuery();
if(rs.next())
{
count = rs.getInt(1);
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {
rs.close();
pre.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
public boolean Delete(int id)
{
int count =0;
try
{
pre = con.prepareStatement("delete from Products where PID="+id+"");
count = pre.executeUpdate();
pre.close();
}catch(Exception e)
{
e.printStackTrace();
}
if(count>0)
{
return true;
}else
{
return false;
}
}
public List<Products> selectProductsByPID(int pid) {
List<Products> list = new ArrayList<Products>();
try
{
dao d = new dao();
con = d.getCon();
if(con!=null)
{
pre = con.prepareStatement("select * from Products where PID="+pid+"");
rs = pre.executeQuery();
while(rs.next())
{
Products pro = new Products();
pro.setPid(rs.getInt(1));
pro.setPname(rs.getString(2));
pro.setPprice(rs.getDouble(3));
pro.setSmallcid(rs.getInt(4));
pro.setAmount(rs.getInt(5));
pro.setPicturepath(rs.getString(6));
pro.setPstatus(rs.getString(7));
list.add(pro);
}
}
rs.close();
pre.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public List<Products> selectProductsByPName(String pname) {
List<Products> list = new ArrayList<Products>();
try
{
dao d = new dao();
con = d.getCon();
if(con!=null)
{
pre = con.prepareStatement("select * from Products where PName="+pname+"");
rs = pre.executeQuery();
while(rs.next())
{
Products pro = new Products();
pro.setPname(rs.getString(2));
pro.setPprice(rs.getDouble(3));
pro.setSmallcid(rs.getInt(4));
pro.setAmount(rs.getInt(5));
pro.setPstatus(rs.getString(6));
list.add(pro);
}
}
rs.close();
pre.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public List<Products> selectProductsBySCID(int scid) {
List<Products> list = new ArrayList<Products>();
try
{
dao d = new dao();
con = d.getCon();
if(con!=null)
{
pre = con.prepareStatement("select * from Products where SmallCID="+scid+"");
rs = pre.executeQuery();
while(rs.next())
{
Products pro = new Products();
pro.setPid(rs.getInt(1));
pro.setPname(rs.getString(2));
pro.setPprice(rs.getDouble(3));
pro.setSmallcid(rs.getInt(4));
pro.setAmount(rs.getInt(5));
pro.setPstatus(rs.getString(6));
list.add(pro);
}
}
rs.close();
pre.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public boolean modifyProductsByPID(int pid,String status) {
int count =0;
try
{
dao d = new dao();
con = d.getCon();
if(con!=null)
{
pre = con.prepareStatement("update Products set PStatus='"
+ status.trim() + "' where PID=" + pid + "");
count = pre.executeUpdate();
}
pre.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
if(count>0)
{
return true;
}else
{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -