課程
/后端開(kāi)發(fā)
/Java
/JAVA遇見(jiàn)HTML——Servlet篇
我想知道dao和items里面代碼怎么寫(xiě)的,要不無(wú)從下手,求dao和items的代碼
2016-11-04
源自:JAVA遇見(jiàn)HTML——Servlet篇 2-7
正在回答
package?entity; //商品類(lèi) public?class?Items?{ private?int?id;?//?商品編號(hào) private?String?name;?//?商品名稱(chēng) private?String?city;?//?產(chǎn)地 private?int?price;?//?價(jià)格 private?int?number;?//?庫(kù)存 private?String?picture;?//?商品圖片 //保留此不帶參數(shù)的構(gòu)造方法 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?"商品編號(hào):"+this.getId()+",商品名稱(chēng):"+this.getName(); } }
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import util.DBHelper;
import entity.Items;
//商品的業(yè)務(wù)邏輯類(lèi)
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語(yǔ)句
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);// 把一個(gè)商品加入集合
}
return list; // 返回集合。
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
// 釋放數(shù)據(jù)集對(duì)象
if (rs != null) {
rs.close();
rs = null;
// 釋放語(yǔ)句對(duì)象
if (stmt != null) {
stmt.close();
stmt = null;
// 根據(jù)商品編號(hào)獲得商品資料
public Items getItemsById(int id) {
String sql = "select * from items where id=?;"; // SQL語(yǔ)句
stmt.setInt(1, id);
if (rs.next()) {
return item;
} else {
//獲取最近瀏覽的前五條商品信息
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
舉報(bào)
本門(mén)課程在JSP課程的基礎(chǔ)上,深入介紹Servlet的基礎(chǔ)知識(shí)
3 回答我實(shí)在搞不清楚this用法
1 回答Items報(bào)錯(cuò)
3 回答Set<Items> keys = goods.keySet(); 什么意思
1 回答商品遍歷不是很理解 Set<Map.Entry<Items, Integer>>
2 回答關(guān)于set<Items>報(bào)錯(cuò)的問(wèn)題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-11-05
2016-11-05
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import util.DBHelper;
import entity.Items;
//商品的業(yè)務(wù)邏輯類(lèi)
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語(yǔ)句
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);// 把一個(gè)商品加入集合
}
return list; // 返回集合。
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
// 釋放數(shù)據(jù)集對(duì)象
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 釋放語(yǔ)句對(duì)象
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
// 根據(jù)商品編號(hào)獲得商品資料
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語(yǔ)句
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 {
// 釋放數(shù)據(jù)集對(duì)象
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 釋放語(yǔ)句對(duì)象
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;
}
}
}