package dao;
import java.sql.*;
import java.util.*;
import entity.*;
public class ShopDao extends BaseDao{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public List listOrderByItem(String item) {
List list=new ArrayList();
if(item==null)
{
item="name";
}
String sql = "select*from Product order by"+item;
System.out.println(sql);
try {
conn = getCon();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next())
{
Product p=new Product();
p.setId(rs.getInt("productID"));
p.setNum(rs.getString("serialNumber"));
p.setName(rs.getString("name"));
p.setBrand(rs.getString("brand"));
p.setModel(rs.getString("model"));
p.setPrice(rs.getDouble("price"));
p.setPicture(rs.getString("picture"));
p.setDescription(rs.getString("description"));
list.add(p);
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
this.closeAll(conn, pstmt, rs);
}
return list;
}
public List listOrderByItem(String item, int page) {
// TODO Auto-generated method stub
List list=new ArrayList();
int rowBegin=0;
if(item==null)
{
item="name";
}
if (page > 1) {
rowBegin = 10 * (page - 1); // 按页数取得开始行数,设每页可以显示5条
}
String sql = "select top 10 * from Product "
+ " where productID not in(select top " + rowBegin
+ " productID from Product "
+ " order by "+item+ " asc ) order by "+ item+ " asc";
System.out.println(sql);
try {
conn = getCon();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next())
{
Product p=new Product();
p.setId(rs.getInt("productID"));
p.setNum(rs.getString("serialNumber"));
p.setName(rs.getString("name"));
p.setBrand(rs.getString("brand"));
p.setModel(rs.getString("model"));
p.setPrice(rs.getDouble("price"));
p.setPicture(rs.getString("picture"));
p.setDescription(rs.getString("description"));
list.add(p);
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
this.closeAll(conn, pstmt, rs);
}
return list;
}
//根据 id 获得一条信息
public Product getPro(int id) {
String sql = "select*from Product where productID="+id;
System.out.println(sql);
Product p=new Product();
try {
conn = getCon();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next())
{
p.setId(rs.getInt("productID"));
p.setNum(rs.getString("serialNumber"));
p.setName(rs.getString("name"));
p.setBrand(rs.getString("brand"));
p.setModel(rs.getString("model"));
p.setPrice(rs.getDouble("price"));
p.setPicture(rs.getString("picture"));
p.setDescription(rs.getString("description"));
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
this.closeAll(conn, pstmt, rs);
}
return p;
}
//通过实体类中的 id 修改一条信息
public int updatePro(Product p)
{
int num=0;
String sql="update Product set serialNumber=?,name=?,brand=?,model=?,price=?,picture=?,description=? where productID="+p.getId();
try {
conn=getCon();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, p.getNum());
pstmt.setString(2,p.getName());
pstmt.setString(3, p.getBrand());
pstmt.setString(4, p.getModel());
pstmt.setDouble(5, p.getPrice());
pstmt.setString(6, p.getPicture());
pstmt.setString(7, p.getDescription());
num=pstmt.executeUpdate();
if(num>0)
System.out.println("修改成功");
else
System.out.println("修改失败");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return num;
}
//通过实体类添加一条信息
public int addPro(Product p)
{
int num=0;
String sql="insert into Product values(?,?,?,?,?,?,?)";
try {
conn=getCon();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, p.getName());
pstmt.setString(2,p.getName());
pstmt.setString(3, p.getBrand());
pstmt.setString(4, p.getModel());
pstmt.setDouble(5, p.getPrice());
pstmt.setString(6, p.getPicture());
pstmt.setString(7, p.getDescription());
num=pstmt.executeUpdate();
if(num>0)
System.out.println("添加成功");
else
System.out.println("添加失败");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return num;
}
//通过id删除记录
public int delPro(int id)
{
int num=0;
String sql="delete Product where productID="+id;
try {
conn=getCon();
pstmt=conn.prepareStatement(sql);
num=pstmt.executeUpdate();
if(num>0)
System.out.println("删除成功");
else
System.out.println("删除失败");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return num;
}
}