1、實現商品分類在前臺主頁顯示java
一、數據庫添加typeGoods表,typeId char(2),typeName varchar(20)sql
二、Bean設計TypeGoods.java數據庫
package mybean;jsp
public class TypeGoods {
private String typeId;
private String typeName;
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
ide
}sqlserver
三、TypeGoodsDAO類的list方法post
package mybean;this
import java.sql.*;
import java.util.LinkedList;spa
public class TypeGoodsDAO {
public LinkedList<TypeGoods> list()
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from typeGoods";
LinkedList<TypeGoods> ts=new LinkedList<TypeGoods>();
try{
Class.forName(Const.DRIVER);
conn=DriverManager.getConnection(Const.URL, Const.USER,Const.PASSWORD);
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
TypeGoods t=new TypeGoods();
t.setTypeId(rs.getString("typeId"));
t.setTypeName(rs.getString("typeName"));
ts.add(t);
}
}catch(Exception e){
e.printStackTrace();
}
try {
if(rs!=null)
{rs.close();}
if(ps!=null)
{ps.close();}
if(conn!=null)
{conn.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ts;
}設計
}
說明:爲實現通用化,將經常使用的變量,定義爲常量存儲於Const類中
package mybean;
public class Const {
public final static String URL="jdbc:sqlserver://localhost:1433;database=ebuy";
public final static String USER="shopping";
public final static String PASSWORD="shopping";
public final static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
四、前臺首頁顯示left.jsp部分代碼
<ul class="list_con">
<%
TypeGoodsDAO tdao=new TypeGoodsDAO();
LinkedList<TypeGoods> ts=tdao.list();
for(TypeGoods t:ts)
{
%>
<li><a href="index.jsp?no=1&type=<%=t.getTypeId()%>"><%=t.getTypeName() %> </a></li>
<%} %>
</ul>
五、菜單欄顯示商品類別
2、商品分類顯示
一、GoodsDAO中添加search方法
Class.forName(Const.DRIVER);
conn=DriverManager.getConnection(Const.URL, Const.USER, Const.PASSWORD);
ps=conn.prepareStatement(sql);
ps.setString(1,type);//1
rs=ps.executeQuery();
while(rs.next()){
Goods g=new Goods();
g.setGid(rs.getInt("gid"));
g.setName(rs.getString("name"));
g.setPrice(rs.getFloat("price"));
g.setNum(rs.getInt("num"));
g.setContent(rs.getString("content"));
g.setPicture(rs.getString("picture"));
gs.add(g);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null)
{rs.close();}
if(ps!=null)
{ps.close();}
if(conn!=null)
{conn.close();}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gs;
}
二、right頁肯定是顯示部分商品仍是顯示所有商品
<% String no=request.getParameter("no");
String type=request.getParameter("type");
String txt="";
if(no==null||"1".equals(no))
{
GoodsDAO dao=new GoodsDAO();
LinkedList<Goods> gs=null;
if(no==null){
txt="所有商品";
gs=dao.list();
}else if(no.equals("1")){
System.out.println(type);
gs=dao.search(type);
txt="部分商品";
}
%>
<div class="current">
<p>當前位置:首頁><%=txt %></p>
</div>
<div class="right_down">
<%
for(Goods g:gs){
%>
<div class="post">
<a href="detail.jsp?gid=<%=g.getGid()%>"><img src="<%=g.getPicture()%>"/></a>
<ul>
<li>名稱:<a href="detail.jsp?gid=<%=g.getGid()%>"><%=g.getName() %></a></li>
<li>價格:<%=g.getPrice() %></li>
<li>庫存數量:<%=g.getNum() %>
</ul>
</div>
<%}
}%>
</div>
三、顯示商品詳細信息detail.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<%
String gid=request.getParameter("gid");
GoodsDAO dao=new GoodsDAO();
Goods g=dao.getById(Integer.parseInt(gid));
%>
<div class="detail">
<form method="GET" action="doCar.jsp">
<img src="p_w_picpaths/product.jpg">
<ul>
<li>編號:<%=g.getGid() %></li>
<li>名稱:<%=g.getName() %></li>
<li>價格:<%=g.getPrice() %></li>
<li><a href="comment.jsp?gid=<%=g.getGid()%>">查看評價</a></li>
<li>數量:<input name="num" size="4" type="text">
<input value="購買" type="submit"></li>
<input name="gid" value="<%=g.getGid()%>" type="hidden">
<input name="action" value="buy" type="hidden">
</ul>
</form>
</div>
<div class="describe">商品描述:<%=g.getContent()%></div>
說明:咱們能夠看到詳細信息是在獨立頁顯示的,嘗試實如今首頁的右側顯示
四、實現詳細信息在右側顯示
right頁更改
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<% String no=request.getParameter("no");
String type=request.getParameter("type");
String gid=request.getParameter("gid");
String txt="";
if(no==null||"1".equals(no))
{
GoodsDAO dao=new GoodsDAO();
LinkedList<Goods> gs=null;
if(no==null){
txt="所有商品";
gs=dao.list();
}else if(no.equals("1")){
System.out.println(type);
gs=dao.search(type);
txt="部分商品";
}
%>
<div class="current">
<p>當前位置:首頁><%=txt %></p>
</div>
<div class="right_down">
<%
for(Goods g:gs){
%>
<div class="post">
<a href="index.jsp?no=detail&gid=<%=g.getGid()%>"><img src="<%=g.getPicture()%>"/></a>
<ul>
<li>名稱:<a href="index.jsp?no=detail&gid=<%=g.getGid()%>"><%=g.getName() %></a></li>
<li>價格:<%=g.getPrice() %></li>
<li>庫存數量:<%=g.getNum() %>
</ul>
</div>
<%}
}
if("detail".equals(no)){
txt="詳細信息";
%>
<jsp:include page="detail.jsp">
<jsp:param value="<%=gid%>" name="gid"/>
</jsp:include>
<%
}
%>
</div> <div class="clear"></div>