favoriteprodao.java

来自「模拟网上购物系统」· Java 代码 · 共 128 行

JAVA
128
字号
package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import po.productPO;
import vo.favproVO;

public class FavoriteProDAO {
	
	private Connection conn = null;
	private Statement state = null;
	private ResultSet rs = null;
	
    //添加
	public boolean addPro(favproVO vo)
	{
		boolean isok = false;
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			int i = state.executeUpdate("insert into favoriteproduct values("+vo.getUserid()+","+vo.getProid()+",'"+vo.getProname()+"',"+vo.getPrice()+","+vo.getMemberprice()+",'"+vo.getPicture()+"')");
		    if(i > 0)
		    {   
		    	isok = true;
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				    rs.close();
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return isok;
	}
	
	public ArrayList findAll(int userid )
	{
		ArrayList array = new ArrayList();
		
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			rs = state.executeQuery("select * from favoriteproduct where userid = " + userid );
			
			while(rs.next())
			{
				favproVO type = new favproVO();
				type.setUserid(rs.getInt("userid"));
		    	type.setProid(rs.getInt("proid"));
		    	type.setProname(rs.getString("proname"));
		    	type.setPrice(rs.getInt("proprice"));
		    	type.setMemberprice(rs.getInt("memberprice"));
		    	type.setPicture(rs.getString("picture"));
		    	
		    	array.add(type);
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				    rs.close();
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return array;
	}
	
//	删除商品
	public boolean deletePro(int id)
	{
		boolean isok = false;
		conn = Tools.getConnection();
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			int i = state.executeUpdate("delete from favoriteproduct where proid ="+id);
		    if(i > 0)
		    {   
		    	isok = true;
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				    rs.close();
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return isok;
	}

}

⌨️ 快捷键说明

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