⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cartdb.java

📁 用java做的网上商店系统!代码完整。希望为大家有所帮助!
💻 JAVA
字号:
package org.qhit.shop.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDB {
	//将商品id存入购物车
//	public int add(int id,int uid){
//		int i=0;
//		Connection conn=null;
//		PreparedStatement ps=null;
//		String sql="insert into cart values(?,?)";
//		try {
//			//1.连接
//			conn=DBConn.getConn();
//			//2.创建预编译指令集对象
//			ps=conn.prepareStatement(sql);
//			//3.绑定参数(和?对应的)
//			ps.setInt(1,id);	
//			ps.setInt(2,uid);
//			//4.执行
//			i=ps.executeUpdate();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			try {
//				if(ps!=null)ps.close();
//				DBConn.close(conn);
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}		
//		return i;
//	}
	//购物时调用的方法,能加,能改
	public int add(int id,int uid){
		int i=0;
		Connection conn=null;
		PreparedStatement ps1=null;
		PreparedStatement ps2=null;
		ResultSet rs=null;
		String sql1="select goodsId from cart where uid=? and state=1";
		String sql="insert into cart values(?,?,1,1,null)";
		String sql2="update cart set sl=sl+1 where goodsId=? and uid=?";
		try {
			//1.连接
			conn=DBConn.getConn();
			//2.创建预编译指令集对象
			ps1=conn.prepareStatement(sql1);
			//3.绑定参数(和?对应的)
			ps1.setInt(1,uid);	
			//4.执行
			rs=ps1.executeQuery();
			boolean b=false;//标志(是否重复)
			while(rs.next()){
				if(id==rs.getInt(1)){
					b=true;//重复
				}
			}
			if(b==true){//重复,修改数量
				ps2=conn.prepareStatement(sql2);	
			}else{//新增
				ps2=conn.prepareStatement(sql);					
			}
			//3.绑定参数(和?对应的)
			ps2.setInt(1,id);
			ps2.setInt(2,uid);
			i=ps2.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(ps1!=null)ps1.close();
				if(ps2!=null)ps2.close();
				DBConn.close(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		return i;
	}
	//查询所有购物车中的
	public List queryAll(int uid){
		
		List all=new ArrayList();
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		GoodsBean gb=null;
		String sql="select c.id,c.goodsId,g.gName,g.gPrice1,g.gPrice2,g.gPic,c.sl from cart as c,goods as g where c.goodsId=g.id and c.uid=? and c.state=1";
		try {
			//1.创建连接
			conn=DBConn.getConn();
			//2.创建指令集对象
			ps=conn.prepareStatement(sql);
			ps.setInt(1, uid);
			//3.执行SQL指令
			rs=ps.executeQuery();
			//4.遍历rs
			while(rs.next()){
				gb=new GoodsBean();
				gb.setC_id(rs.getInt(1));
				gb.setId(rs.getInt(2));
				gb.setGName(rs.getString(3));
				gb.setGPrice1(rs.getDouble(4));
				gb.setGPrice2(rs.getDouble(5));				
				gb.setGPic(rs.getString(6));
				gb.setGmsl(rs.getInt(7));
				all.add(gb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)rs.close();
				if(ps!=null)ps.close();
				DBConn.close(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return all;
	}
	//从购物车中删除商品
	public int remove(int id){
		int i=0;
		Connection conn=null;
		PreparedStatement ps=null;
		String sql="delete from cart where id=?";
		try {
			//1.连接
			conn=DBConn.getConn();
			//2.创建预编译指令集对象
			ps=conn.prepareStatement(sql);
			//3.绑定参数(和?对应的)
			ps.setInt(1,id);		
			//4.执行
			i=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)ps.close();
				DBConn.close(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		return i;
	}
	//修改购物车中商品数量的方法
	public int update(int id,int uid,int sl){
		int i=0;
		Connection conn=null;
		PreparedStatement ps=null;
		String sql="update cart set sl=? where goodsId=? and uid=?";
		try {
			//1.连接
			conn=DBConn.getConn();
			//2.创建预编译指令集对象
			ps=conn.prepareStatement(sql);
			//3.绑定参数(和?对应的)
			ps.setInt(1,sl);
			ps.setInt(2,id);
			ps.setInt(3,uid);
			//4.执行
			i=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)ps.close();
				DBConn.close(conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		return i;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -