<%@?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>
??????????<!--?商品循環開始?-->
??????????<%
????????????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">產地:<%=item.getCity()?%> 價格:¥?<%=item.getPrice()?%></dd>
????????????</dl>
??????????</div>
??????????<!--?商品循環結束?-->
??????????<%
??????????????}
????????????}
??????????%>
????????</td>
??????</tr>
????</table>
??</center>
??</body>
</html>
public?class?Item?{
????private?int?id;?//?商品編號
????private?String?name;?//?商品名稱
????private?String?city;?//?產地
????private?int?price;?//?價格
????private?int?number;?//?庫存
????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?{
????????????//?釋放數據集對象
????????????if?(rs?!=?null)?{
????????????????try?{
????????????????????rs.close();
????????????????????rs?=?null;
????????????????}?catch?(Exception?ex)?{
????????????????????ex.printStackTrace();
????????????????}
????????????}
????????????//?釋放語句對象
????????????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?{
????????????//?釋放數據集對象
????????????if?(rs?!=?null)?{
????????????????try?{
????????????????????rs.close();
????????????????????rs?=?null;
????????????????}?catch?(Exception?ex)?{
????????????????????ex.printStackTrace();
????????????????}
????????????}
????????????//?釋放語句對象
????????????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("連接失敗");
????}
}
2017-08-29
你的 ItemsDao類中的?getItems()方法中的 item你創建的時候寫的是:“Item?item?=?null;”這樣你給 item實例 的屬性賦值調用 Item類 的方法的時候,系統認不出來這個?item引用 是 Item類型 的,所以無法賦值會直接報錯,導致程序并沒有執行,所以你的 items數組?中其實什么也沒加進去是空的;
所以你的創建item是應該調用?Item類的構造方法:“Item item = new Item();”,這樣系統才可以認出這個 item引用 是 Item類型 的引用;
另外while循環中最后一行,你把item引用置為空了而且又沒有新創建一個 Item類型的實例,這樣你第二次執行循環會出同樣的錯,而 ArrayList的add?存的只是對象的引用,并不是把對象內容重新copy一份再把新引用存進去,如果引用所指向的實例內容發生變化的話,ArrayList中引用指向的實例內容也隨之發生變化。
所以?你應該把 item實例 的創建寫進循環內部,每次循環都創建一個 item新實例,再進行賦值并添加到 items數組 中;
如果不是每次循環每次新建的話,你最后 ?items數組 中存的其實是同一個item的引用,也就是說雖然有十條繩子,但是牽的是一條狗。最終會導致 items數組 中的內容都一樣,頁面里顯示的是同一個商品,只不過顯示了十個。
2017-08-30
你index.jsp中 item的創建 寫的也是 Item item = null;
你 index.jsp 第一行的寫法和文件導入的寫法,不清楚你寫的對不對,可以參考看下老師的:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="entity.Items"%>
<%@ page import="dao.ItemsDAO"%>
2017-08-29
你數據庫里有數據么?