第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

為什么我jsp頁(yè)面顯示不了商品呢?

<%@?page?contentType="text/html;charset=UTF-8"?language="java"?%>
<%@?page?import="com.bean.Item"?%>
<%@?page?import="com.dao.ItemsDao"?%>
<%@?page?import="java.util.List"?%>
<html>
??<head>
????<title>商品列表</title>
??????<style?type="text/css">
??????????div{
??????????????float:left;
??????????????margin:?10px;
??????????}
??????????div?dd{
??????????????margin:0px;
??????????????font-size:10pt;
??????????}
??????????div?dd.dd_name
??????????{
??????????????color:blue;
??????????}
??????????div?dd.dd_city
??????????{
??????????????color:#000;
??????????}
??????</style>
??</head>
??<body>
??<h1>商品展示</h1>
??<hr>
??<center>
????<table?width="750"?height="60"?cellpadding="0"?cellspacing="0"?border="0">
??????<tr>
????????<td>

??????????<!--?商品循環(huán)開始?-->
??????????<%
????????????ItemsDao?itemsDao?=?new?ItemsDao();
????????????List<Item>?items?=??itemsDao.getItems();
????????????Item?item?=?null;
????????????if?(items!=null&&items.size()>0)?{
??????????????for?(int?i?=0;i<items.size();i++){
??????????????????item?=?items.get(i);
??????????%>
??????????<div>
????????????<dl>
??????????????<dt>
????????????????<a?href="details.jsp?id=<%=item.getId()%>"><img?src="images/<%=item.getPicture()%>"?width="120"?height="90"?border="1"/></a>
??????????????</dt>
??????????????<dd?class="dd_name"><%=item.getName()?%></dd>
??????????????<dd?class="dd_city">產(chǎn)地:<%=item.getCity()?%>&nbsp;&nbsp;價(jià)格:¥?<%=item.getPrice()?%></dd>
????????????</dl>
??????????</div>
??????????<!--?商品循環(huán)結(jié)束?-->

??????????<%
??????????????}
????????????}
??????????%>
????????</td>
??????</tr>
????</table>
??</center>
??</body>
</html>
public?class?Item?{

????private?int?id;?//?商品編號(hào)
????private?String?name;?//?商品名稱
????private?String?city;?//?產(chǎn)地
????private?int?price;?//?價(jià)格
????private?int?number;?//?庫(kù)存
????private?String?picture;?//?商品圖片

????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;
????}
}
public?class?ItemsDao?{

????public?List<Item>?getItems()?{
????????Connection?conn?=?null;
????????Item?item?=?null;
????????ResultSet?rs?=null;
????????List<Item>?items?=?new?ArrayList<Item>();
????????conn?=?DBHelper.getConn();
????????String?sql?=?"?select?*?from?items?";
????????PreparedStatement?ps?=?null;
????????try?{
????????????ps?=?conn.prepareStatement(sql);
????????????rs?=?ps.executeQuery();
????????????while?(rs.next()){
????????????????item.setId(rs.getInt("id"));
????????????????item.setName(rs.getString("name"));
????????????????item.setCity(rs.getString("city"));
????????????????item.setNumber(rs.getInt("number"));
????????????????item.setPicture(rs.getString("picture"));
????????????????item.setPrice(rs.getInt("price"));
????????????????items.add(item);
????????????????item?=?null;
????????????}
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????????return?null;
????????}finally?{
????????????//?釋放數(shù)據(jù)集對(duì)象
????????????if?(rs?!=?null)?{
????????????????try?{
????????????????????rs.close();
????????????????????rs?=?null;
????????????????}?catch?(Exception?ex)?{
????????????????????ex.printStackTrace();
????????????????}
????????????}
????????????//?釋放語(yǔ)句對(duì)象
????????????if?(ps?!=?null)?{
????????????????try?{
????????????????????ps.close();
????????????????????ps?=?null;
????????????????}?catch?(Exception?ex)?{
????????????????????ex.printStackTrace();
????????????????}
????????????}
????????}
????????return?items;
????}

????public?Item?getItem(?int?id)?{
????????Connection?conn?=?null;
????????Item?item?=?null;
????????ResultSet?rs?=null;
????????conn?=?DBHelper.getConn();
????????String?sql?=?"?select?*?from?items?WHERE?id?=???";
????????PreparedStatement?ps?=?null;
????????try?{
????????????ps?=?conn.prepareStatement(sql);
????????????ps.setInt(1,id);
????????????rs?=?ps.executeQuery();
????????????if(rs.next()){
????????????????item.setId(rs.getInt("id"));
????????????????item.setName(rs.getString("name"));
????????????????item.setCity(rs.getString("city"));
????????????????item.setNumber(rs.getInt("number"));
????????????????item.setPicture(rs.getString("picture"));
????????????????item.setPrice(rs.getInt("price"));
????????????????return?item;
????????????}else
????????????????return?null;
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????????return?null;
????????}finally?{
????????????//?釋放數(shù)據(jù)集對(duì)象
????????????if?(rs?!=?null)?{
????????????????try?{
????????????????????rs.close();
????????????????????rs?=?null;
????????????????}?catch?(Exception?ex)?{
????????????????????ex.printStackTrace();
????????????????}
????????????}
????????????//?釋放語(yǔ)句對(duì)象
????????????if?(ps?!=?null)?{
????????????????try?{
????????????????????ps.close();
????????????????????ps?=?null;
????????????????}?catch?(Exception?ex)?{
????????????????????ex.printStackTrace();
????????????????}
????????????}
????????}
????}
}
public?class?DBHelper?{

????private?static?Connection?conn;

????static?{
????????try?{
????????????Class.forName("com.mysql.jdbc.Driver");
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}

????public?static?Connection?getConn(){
????????if?(conn==null)
????????????try?{
????????????????conn?=?DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc",
????????????????????????"root",?"2503391049");
????????????}?catch?(SQLException?e)?{
????????????????e.printStackTrace();
????????????}
????????return?conn;
????}

????public?static?void?main(String[]?args)?{
????????Connection?connection?=?getConn();
????????if?(connection!=null)
????????????System.out.println("連接成功");
????????else
????????????System.out.println("連接失敗");
????}

}


正在回答

3 回答

你的 ItemsDao類中的?getItems()方法中的 item你創(chuàng)建的時(shí)候?qū)懙氖牵骸癐tem?item?=?null;”這樣你給 item實(shí)例 的屬性賦值調(diào)用 Item類 的方法的時(shí)候,系統(tǒng)認(rèn)不出來(lái)這個(gè)?item引用 是 Item類型 的,所以無(wú)法賦值會(huì)直接報(bào)錯(cuò),導(dǎo)致程序并沒有執(zhí)行,所以你的 items數(shù)組?中其實(shí)什么也沒加進(jìn)去是空的;

所以你的創(chuàng)建item是應(yīng)該調(diào)用?Item類的構(gòu)造方法:“Item item = new Item();”,這樣系統(tǒng)才可以認(rèn)出這個(gè) item引用 是 Item類型 的引用;

另外while循環(huán)中最后一行,你把item引用置為空了而且又沒有新創(chuàng)建一個(gè) Item類型的實(shí)例,這樣你第二次執(zhí)行循環(huán)會(huì)出同樣的錯(cuò),而 ArrayList的add?存的只是對(duì)象的引用,并不是把對(duì)象內(nèi)容重新copy一份再把新引用存進(jìn)去,如果引用所指向的實(shí)例內(nèi)容發(fā)生變化的話,ArrayList中引用指向的實(shí)例內(nèi)容也隨之發(fā)生變化。

所以?你應(yīng)該把 item實(shí)例 的創(chuàng)建寫進(jìn)循環(huán)內(nèi)部,每次循環(huán)都創(chuàng)建一個(gè) item新實(shí)例,再進(jìn)行賦值并添加到 items數(shù)組 中;

如果不是每次循環(huán)每次新建的話,你最后 ?items數(shù)組 中存的其實(shí)是同一個(gè)item的引用,也就是說(shuō)雖然有十條繩子,但是牽的是一條狗。最終會(huì)導(dǎo)致 items數(shù)組 中的內(nèi)容都一樣,頁(yè)面里顯示的是同一個(gè)商品,只不過(guò)顯示了十個(gè)。

0 回復(fù) 有任何疑惑可以回復(fù)我~
  1. 你index.jsp中 item的創(chuàng)建 寫的也是 Item item = null;

  2. 你 index.jsp 第一行的寫法和文件導(dǎo)入的寫法,不清楚你寫的對(duì)不對(duì),可以參考看下老師的:

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

    <%@ page import="entity.Items"%>

    <%@ page import="dao.ItemsDAO"%>

0 回復(fù) 有任何疑惑可以回復(fù)我~

你數(shù)據(jù)庫(kù)里有數(shù)據(jù)么?

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

moummm 提問(wèn)者

數(shù)據(jù)庫(kù)里面有數(shù)據(jù),但是在循環(huán)中加入了 new Item() 還是一樣沒有商品顯示。(我這運(yùn)行的時(shí)候程序并沒有報(bào)錯(cuò)。。。)
2017-08-29 回復(fù) 有任何疑惑可以回復(fù)我~
#2

二部學(xué)習(xí)小組 回復(fù) moummm 提問(wèn)者

你沒有把已經(jīng)完成添加的 items數(shù)組 return 回去;再 try 的最后一行且在循環(huán)結(jié)束后(也就是循環(huán)外面),把執(zhí)行完的 items數(shù)組return回去;如:“return items;”
2017-08-30 回復(fù) 有任何疑惑可以回復(fù)我~
#3

二部學(xué)習(xí)小組 回復(fù) moummm 提問(wèn)者

你 return 寫在 finally 里不一點(diǎn)起作用的,這個(gè)問(wèn)題就深?yuàn)W了,自己百度吧……
2017-08-30 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

為什么我jsp頁(yè)面顯示不了商品呢?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)