📄 productbean.java
字号:
package a;
import java.sql.*;
import java.io.*;
import java.util.*;
public class ProductBean
{
private Connection con;
public ProductBean()
{
con=DataBaseConnection.getConnection();
}
//查询所有商品信息
public Collection QueryAllProduct()
{
Collection ret=new ArrayList();
try
{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select*from productInfo");
while(rs.next())
{
ProductInfo info=new ProductInfo();
info.setId(rs.getString("id"));
info.setKindId(rs.getString("kindId"));
info.setName(rs.getString("name"));
info.setProducer(rs.getString("producer"));
info.setPrice(rs.getFloat("price"));
info.setDescn(rs.getString("descn"));
ret.add(info);
}
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}
//按照商品类别查询
public Collection QueryByKindProduct(String kindId)
{
Collection ret=new ArrayList();
try
{
//Statement stmt=con.createStatement();
//ResultSet rs=stmt.executeQuery("select*from info where kind_id='"+kind_id+"'");
PreparedStatement pstmt=con.prepareStatement("select*from productInfo where kindId=?");
pstmt.setString(1,kindId);
ResultSet rs=pstmt.executeQuery();
while(rs.next())
{
ProductInfo info=new ProductInfo();
info.setId(rs.getString("id"));
info.setKindId(rs.getString("kindId"));
info.setName(rs.getString("name"));
info.setProducer(rs.getString("producer"));
info.setPrice(rs.getFloat("price"));
info.setDescn(rs.getString("descn"));
ret.add(info);
}
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}
//按照商品ID查询
public Collection QueryByIdProduct(String id)
{
Collection ret=new ArrayList();
try
{
PreparedStatement pstmt=con.prepareStatement("select*from productInfo where id=?");
pstmt.setString(1,id);
ResultSet rs=pstmt.executeQuery();
while(rs.next())
{
ProductInfo info=new ProductInfo();
info.setId(rs.getString("id"));
info.setKindId(rs.getString("kindId"));
info.setName(rs.getString("name"));
info.setProducer(rs.getString("producer"));
info.setPrice(rs.getFloat("price"));
info.setDescn(rs.getString("descn"));
ret.add(info);
}
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}
//添加一个商品信息
public void addProduct(ProductInfo info)
{
try
{
PreparedStatement pstmt=con.prepareStatement("insert into productInfo values(?,?,?,?,?,?)");
pstmt.setString(1,info.getId());
pstmt.setString(2,info.getKindId());
pstmt.setString(3,info.getName());
pstmt.setString(4,info.getProducer());
pstmt.setFloat(5,info.getPrice());
pstmt.setString(6,info.getDescn());
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//更改商品信息
public void modifyProduct(ProductInfo info)
{
try
{
String up="update productInfo set kindId=?,name=?,producer=?,price=?,descn=? where id=?";
PreparedStatement pstmt=con.prepareStatement(up);
pstmt.setString(1,info.getKindId());
pstmt.setString(2,info.getName());
pstmt.setString(3,info.getProducer());
pstmt.setFloat(4,info.getPrice());
pstmt.setString(5,info.getDescn());
pstmt.setString(6,info.getId());
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//删除指定ID的商品信息
public void deleteProduct(String id)
{
try
{
PreparedStatement pstmt=con.prepareStatement("delete from productInfo where id=?");
pstmt.setString(1,id);
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//更改返回指定ID的商品信息
public ProductInfo getProductInfo(String id)throws Exception
{
PreparedStatement pstmt=con.prepareStatement("select*from productInfo where id=?");
pstmt.setString(1,id);
ResultSet rs=pstmt.executeQuery();
ProductInfo info=new ProductInfo();
while(rs.next())
{
info.setId(rs.getString("id"));
info.setKindId(rs.getString("kindId"));
info.setName(rs.getString("name"));
info.setProducer(rs.getString("producer"));
info.setPrice(rs.getFloat("price"));
info.setDescn(rs.getString("descn"));
}
pstmt.close();
con.close();
return info;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -