相关知识:
JSP+JavaBean开发模式
一、商品信息添加“商品类别”项type,1--饮品 2--手机 3--笔记本(1表示饮品)
1、数据库添加商品类别项
2、JavaBean中添加商品类别项
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
二、商品信息添加功能实现
(1)商品信息添加页面设计good_add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="good_doAdd.jsp">
<table width="500" border="1">
<tr>
<td>商品名称</td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>商品描述</td>
<td><textarea name="content" id="content" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" id="price" /></td>
</tr>
<tr>
<td>商品数量</td>
<td><input type="text" name="num" id="num" /></td>
</tr>
<tr>
<td>商品类型</td>
<td><select name="type" id="type">
<option>商品类型</option>
<option value="1">饮品</option>
<option value="2">手机</option>
<option value="3">笔记本</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
(2)DAO.add()实现商品信息添加
public boolean add(Goods g){
boolean flag=false;
String url="jdbc:sqlserver://10.40.43.202:1433;database=ebuy";
String user="shopping";
String password="shopping";
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String sql="insert into goods(name,price,num,type)values(?,?,?,?)";
Connection conn=null;
PreparedStatement stmt=null;
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, password);
//Statement子类,具有预处理SQL语句功能个
stmt=conn.prepareStatement(sql);
stmt.setString(1, g.getName());
stmt.setFloat(2, g.getPrice());
stmt.setInt(3, g.getNum());
stmt.setString(4, g.getType());
int n=stmt.executeUpdate();
if(n>=1)
{
flag=true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
}catch(Exception e1){
}
return flag;
}
(3)JSP文件访问DAO.add()实现添加商品good_doAdd.jsp
编写g_save.jsp添加商品处理
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ pageimport="java.sql.*" %>
<%@ pageimport="java.util.*" %>
<%@ page import="mybean.*"%>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="goods"class="mybean.Goods" scope="page"/>
<jsp:getProperty name="goods"property="*"/>
<%
GoodsDAO dao = newGoodsDAO();
dao.add(g);
//斜体部分为修改商品使用,添加商品时写
%>
<jsp:forward page="good_view.jsp"/>
三、商品信息显示及操作页面
说明:
(1)点击“添加”链接--->good_add页面,用户添加信息--->good_doAdd将用户添加信息保存到数据库
(2)点击“删除”链接(传递gid值)--->good_delete,删除数据库中gid相同的商品信息
(3)点击“修改”链接(传递gid值)--->good_update页面(通过gid参数数据库中查询相应商品信息,通过表单方式显示信息,其中gid不可更改)--->good_doUpdate实现数据库中更改信息
(4)点击“详细”链接(传递gid值)--->good.jsp 显示商品详细信息,包括商品描述、商品类型等(表格显示,与商品添加、商品修改页面相似)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="mybean.*" %>
<div style="width:500px;">
<p>商品信息显示</p>
<div style="padding-left:350px;"><a href="good_add.jsp">添加</a></div>
<table width="400" border="1">
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品数量</td>
<td>删除</td>
<td>修改</td>
<td>详细</td>
</tr>
<%
GoodsDAO dao=new GoodsDAO();
LinkedList<Goods>gs=dao.list();
for(Goods g:gs)
{
//out.println(g.getName());
%>
<tr>
<td><%=g.getGid()%></td>
<td><%=g.getName()%></td>
<td><%=g.getPrice()%></td>
<td><%=g.getNum()%></td>
<td><a href="good_delete.jsp?gid=<%=g.getGid()%>">删除</a></td>
<td><a href="good_update.jsp?gid=<%=g.getGid()%>">修改</a></td>
<td>详细</td>
</tr>
<%} %>
</table>
</div>
四、商品信息删除
(1)商品浏览页good_view.jsp添加删除链接< a href="good_delete.jsp?gid=<%=g.getGid()%>">删除</a>
(2)DAO.delete()实现商品信息删除
public boolean delete(int gid)
{
boolean flag=false;
String url="jdbc:sqlserver://10.40.43.202:1433;database=ebuy";
String user="shopping";
String password="shopping";
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//String sql="delete from goods where gid=1001" ;
String sql="delete from goods where gid=?" ;
Connection conn=null;
//Statement stmt=null;
PreparedStatement stmt=null;
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, password);
//stmt=conn.createStatement();
// int n=stmt.executeUpdate(sql);
// if(n>=1)
// {
// flag=true;
// }
//
stmt=conn.prepareStatement(sql);
stmt.setInt(1, gid);
int n=stmt.executeUpdate();
if(n>=1)
{
flag=true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
}catch(Exception e1){
}
return flag;
}
(3)JSP文件访问DAO.delete()实现删除商品信息good_delete.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<%
GoodsDAO dao=new GoodsDAO();
String gid=request.getParameter("gid");
if(dao.delete(Integer.parseInt(gid)))
{
out.print("ok");
}else{
out.print("error");
}
response.setHeader("refresh", "2;url=good_view.jsp");
%>
五、商品信息更改功能实现
(0)DAO实现通过gid获得商品信息
public Goods getById(int gid){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String url="jdbc:sqlserver://10.40.43.202:1433;Database=ebuy";
String user="shopping";
String password="shopping";
String sql="select * from goods where gid=?";
String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
Goods g=new Goods();
try {
Driver driver=(Driver)Class.forName(className).newInstance();
conn=DriverManager.getConnection(url, user, password);
stmt=conn.prepareStatement(sql);
stmt.setInt(1,gid);
rs=stmt.executeQuery();
if(rs.next()){
g.setGid(rs.getInt("gid"));
g.setName(rs.getString("name"));
g.setPrice(rs.getFloat("price"));
g.setNum(rs.getInt("num"));
g.setType(rs.getString("type"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null)
{rs.close();}
if(stmt!=null)
{stmt.close();}
if(conn!=null)
{conn.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return g;
}
(1)商品信息更改页面设计good_update.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<%
String gid=request.getParameter("gid");
GoodsDAO dao=new GoodsDAO();
int id=Integer.parseInt(gid);
Goods g=dao.getById(id);
%>
商品信息修改. <br>
<form id="form1" name="form1" method="post" action="good_doUpdate.jsp">
<table width="500" border="1">
<tr>
<td>商品ID</td>
<td><input type="text" name="name" id="name" value="<%=g.getGid()%>" readonly="ok"/></td><!--只读,不可改商品编号-->
</tr>
<tr>
<td>商品名称</td>
<td><input type="text" name="name" id="name" value="<%=g.getName()%>"/></td>
</tr>
<tr>
<td>商品描述</td>
<td><textarea name="content" id="content" cols="45" rows="5"><%=g.getContent()%></textarea></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" id="price" value="<%=g.getPrice()%>"/></td>
</tr>
<tr>
<td>商品数量</td>
<td><input type="text" name="num" id="num" value="<%=g.getNum()%>"/></td>
</tr>
<tr>
<td>商品类型</td>
<td><select name="type" id="type">
<option>商品类型</option>
<option value="1">饮品</option>
<option value="2">手机</option>
<option value="3">笔记本</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
(2)DAO.update()实现商品信息修改(与add代码基本相同,可以复制,区别是sql语句及相关部分)
public boolean update(Goods g){
boolean flag=false;
String url="jdbc:sqlserver://localhost:1433;database=ebuy";
String user="shopping";
String password="shopping";
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//String sql="insert into goods(name,price,num,type)values(?,?,?,?)";
String sql="update goods set name=?,price=?,num=?,type=?,content=? where gid=?";
Connection conn=null;
PreparedStatement stmt=null;
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, password);
stmt=conn.prepareStatement(sql);
stmt.setString(1, g.getName());
stmt.setFloat(2, g.getPrice());
stmt.setInt(3, g.getNum());
stmt.setString(4, g.getType());
stmt.setString(5,g.getContent());
stmt.setInt(6,g.getGid());
int n=stmt.executeUpdate();
if(n>=1)
{
flag=true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
}catch(Exception e1){
}
return flag;
}
(3)JSP文件访问DAO.update()实现修改商品信息 good_doUpdate.jsp(与doAdd相似,区别执行dao.update)
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="goods" class="mybean.Goods"></jsp:useBean>
<jsp:setProperty property="*" name="goods"/>
<%
GoodsDAO dao=new GoodsDAO();
if(dao.update(goods))
{
out.println("ok");
}else{
out.println("error");
}
response.setHeader("refresh", "2;url=good_view.jsp");
%>
六、实现商品信息查询功能
1、实现按商品类别查询
(1)商品DAO添加分类查询功能
public LinkedList<Goods> search(String type){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
LinkedList<Goods> gs=new LinkedList<Goods>();
String url="jdbc:sqlserver://localhost:1433;database=ebuy";
String user="shopping";
String password="shopping";
String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String sql="select * from goods where type=?";
try {
Class.forName(className);
conn=DriverManager.getConnection(url,user,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"));
}
} 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;
}
(2)good_view修改代码
<div style="padding-left:150px;float:left;">
<form action="" method="post">
<select name="type" id="type">
<option value="">--商品类型--</option>
<option value="1">饮品</option>
<option value="2">手机</option>
<option value="3">笔记本</option>
</select>
<input type="submit" value="查询"/>
</form>
</div>
<div style="padding-right:150px;float:right;"><a href="good_add.jsp">添加</a></div>
(3)实现点击“查询”按分类查询
2、实现按商品名称查询