📄 buycardao.java
字号:
package com.newer.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import com.newer.bean.BuycarBean;
public class BuycarDao {
//------------------将商品添加到购物车----------------------
public boolean addCarInfo(int orderID,int carCount,int loginID){
boolean res=false;
Connection con=ctxUtil.getConn();
PreparedStatement pstmt=null;
String sql="insert into buyCar(orderID,loginID,carCount) values(?,?,?)";
System.out.println("daoorderID:"+orderID);
System.out.println("daologinID:"+loginID);
System.out.println("daocarCount:"+carCount);
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1,orderID);
pstmt.setInt(2,loginID);
pstmt.setInt(3,carCount);
int count=pstmt.executeUpdate();
if(count>0){
res=true;
}
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
res=false;
}
// finally{
// try {
// if(pstmt!=null){
// pstmt.close();
// }
// if(con!=null){
// con.close();
// }
//
// } catch (Exception ex) {
// ex.printStackTrace();
// return false;
// // TODO: handle exception
// }
// }
return res;
}
///------------------将商品添加到购物车结束----------------------
//---------通过购物者编号从购物信息表中获取改用户的所有购物信息-------
public Vector getAllCarInfoByULoginID(int loginID){
Vector vc=new Vector();
Connection con=ctxUtil.getConn();
PreparedStatement pstmt=null;
String sql="select car.carID,car.carCount,car.carAddTime,ord.orderTitle,ord.nowPrice from buyCar car,orders ord where car.orderID=ord.orderID and car.loginID=?";
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, loginID);
ResultSet rs=pstmt.executeQuery();
int vccount=0;
while(rs.next()){
BuycarBean bcb=new BuycarBean();
bcb.setCarID(rs.getInt("carID"));
bcb.setCarCount(rs.getInt("carCount"));
bcb.setCarAddTime(rs.getString("carAddTime"));
bcb.setOrderTitle(rs.getString("orderTitle"));
bcb.setNowPrice(rs.getFloat("nowPrice"));
vc.add(vccount++,bcb);
}
rs.close();
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return vc;
}
///---------通过购物者编号从购物信息表中获取改用户的所有购物信息结束-------
//-------------通过购物者编号清空其购物车中的所有商品信息---------------
public boolean clearAllCarByLoginID(int loginID){
boolean res=false;
Connection con=ctxUtil.getConn();
PreparedStatement pstmt=null;
String sql="delete buyCar where loginID=?";
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1,loginID);
int count=pstmt.executeUpdate();
if(count>0){
res=true;
}
pstmt.close();
con.close();
} catch (Exception e) {
System.out.println("清空购物车失败");
e.printStackTrace();
return false;
}
return res;
}
///----------通过购物者编号清空其购物车中的所有商品信息结束--------------
//--------------根据购物车编号删除购物车中的商品信息-------------------
public boolean delBuycarInfoByCarID(int carID){
boolean res=false;
Connection con=ctxUtil.getConn();
PreparedStatement pstmt=null;
String sql="delete buyCar where carID=?";
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, carID);
int count=pstmt.executeUpdate();
if(count>0){
res=true;
}
pstmt.close();
con.close();
} catch (Exception e) {
System.out.println("根据购物车编号删除购物车中的商品信息失败!");
e.printStackTrace();
return false;
}
return res;
}
///--------------根据购物车编号删除购物车中的商品信息------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -