<%@ 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("连接失败");
}
}