亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

dao和items不清楚

我想知道dao和items里面代碼怎么寫的,要不無從下手,求dao和items的代碼

正在回答

2 回答

package?entity;

//商品類
public?class?Items?{

	private?int?id;?//?商品編號
	private?String?name;?//?商品名稱
	private?String?city;?//?產地
	private?int?price;?//?價格
	private?int?number;?//?庫存
	private?String?picture;?//?商品圖片

	//保留此不帶參數的構造方法
	public?Items()
	{
		
	}
	
	public?Items(int?id,String?name,String?city,int?price,int?number,String?picture)
	{
		this.id?=?id;
		this.name?=?name;
		this.city?=?city;
		this.picture?=?picture;
		this.price?=?price;
		this.number?=?number;
		
	}
	public?int?getId()?{
		return?id;
	}

	public?void?setId(int?id)?{
		this.id?=?id;
	}

	public?String?getName()?{
		return?name;
	}

	public?void?setName(String?name)?{
		this.name?=?name;
	}

	public?String?getCity()?{
		return?city;
	}

	public?void?setCity(String?city)?{
		this.city?=?city;
	}

	public?int?getPrice()?{
		return?price;
	}

	public?void?setPrice(int?price)?{
		this.price?=?price;
	}

	public?int?getNumber()?{
		return?number;
	}

	public?void?setNumber(int?number)?{
		this.number?=?number;
	}

	public?String?getPicture()?{
		return?picture;
	}

	public?void?setPicture(String?picture)?{
		this.picture?=?picture;
	}
	
	
	
	@Override
	public?int?hashCode()?{
		//?TODO?Auto-generated?method?stub
		return?this.getId()+this.getName().hashCode();
	}

	@Override
	public?boolean?equals(Object?obj)?{
		//?TODO?Auto-generated?method?stub
		if(this==obj)
		{
			return?true;
		}
		if(obj?instanceof?Items)
		{
			Items?i?=?(Items)obj;
			if(this.getId()==i.getId()&&this.getName().equals(i.getName()))
			{
				return?true;
			}
			else
			{
				return?false;
			}
		}
		else
		{
			return?false;
		}
	}

	public?String?toString()
	{
		return?"商品編號:"+this.getId()+",商品名稱:"+this.getName();
	}

}


0 回復 有任何疑惑可以回復我~

package dao;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;


import util.DBHelper;


import entity.Items;


//商品的業務邏輯類

public class ItemsDAO {


// 獲得所有的商品信息

public ArrayList<Items> getAllItems() {

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

ArrayList<Items> list = new ArrayList<Items>(); // 商品集合

try {

conn = DBHelper.getConnection();

String sql = "select * from items;"; // SQL語句

stmt = conn.prepareStatement(sql);

rs = stmt.executeQuery();

while (rs.next()) {

Items item = new Items();

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setCity(rs.getString("city"));

item.setNumber(rs.getInt("number"));

item.setPrice(rs.getInt("price"));

item.setPicture(rs.getString("picture"));

list.add(item);// 把一個商品加入集合

}

return list; // 返回集合。

} catch (Exception ex) {

ex.printStackTrace();

return null;

} finally {

// 釋放數據集對象

if (rs != null) {

try {

rs.close();

rs = null;

} catch (Exception ex) {

ex.printStackTrace();

}

}

// 釋放語句對象

if (stmt != null) {

try {

stmt.close();

stmt = null;

} catch (Exception ex) {

ex.printStackTrace();

}

}

}


}


// 根據商品編號獲得商品資料

public Items getItemsById(int id) {

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

conn = DBHelper.getConnection();

String sql = "select * from items where id=?;"; // SQL語句

stmt = conn.prepareStatement(sql);

stmt.setInt(1, id);

rs = stmt.executeQuery();

if (rs.next()) {

Items item = new Items();

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setCity(rs.getString("city"));

item.setNumber(rs.getInt("number"));

item.setPrice(rs.getInt("price"));

item.setPicture(rs.getString("picture"));

return item;

} else {

return null;

}


} catch (Exception ex) {

ex.printStackTrace();

return null;

} finally {

// 釋放數據集對象

if (rs != null) {

try {

rs.close();

rs = null;

} catch (Exception ex) {

ex.printStackTrace();

}

}

// 釋放語句對象

if (stmt != null) {

try {

stmt.close();

stmt = null;

} catch (Exception ex) {

ex.printStackTrace();

}

}


}

}

//獲取最近瀏覽的前五條商品信息

public ArrayList<Items> getViewList(String list)

{

System.out.println("list:"+list);

ArrayList<Items> itemlist = new ArrayList<Items>();

int iCount=5; //每次返回前五條記錄

if(list!=null&&list.length()>0)

{

? ?String[] arr = list.split(",");

? ?System.out.println("arr.length="+arr.length);

? ?//如果商品記錄大于等于5條

? ?if(arr.length>=5)

? ?{

? ? ? for(int i=arr.length-1;i>=arr.length-iCount;i--)

? ? ? {


? ? ?itemlist.add(getItemsById(Integer.parseInt(arr[i]))); ?

? ? ? }

? ?}

? ?else

? ?{

? ? for(int i=arr.length-1;i>=0;i--)

? ? {

? ? itemlist.add(getItemsById(Integer.parseInt(arr[i])));

? ? }

? ?}

? ?return itemlist;

}

else

{

return null;

}

}


}


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

dao和items不清楚

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號