此文章是从
http://blog.sina.com.cn/s/blog_5f6e0a9a0100cv3k.html
转载的,在此,谢谢原作者。
将汉字转为可以识别的光标码
package com;
import java.io.UnsupportedEncodingException;
public class Change {
public static String changetoString(String str){
String returnstr=null;
try {
byte b[]=str.getBytes("ISO-8859-1");
returnstr=new String(b,"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnstr;
}
}
商品类
package com;
public class Product {
private String product_id;
private String product_name;
private float product_price;
public String getProduct_id() {
return product_id;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public float getProduct_price() {
return product_price;
}
public void setProduct_price(float product_price) {
this.product_price = product_price;
}
}
dao处理文件
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class ProductDao {
public Connection conn(){
Connection conn=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=chanpin;user=sa;password=sa";
conn=DriverManager.getConnection(URL);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public List getUserList(){
List list=new ArrayList();
try {
Connection conn=this.conn();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from product");
while(rs.next()){
Product pro=new Product();
String product_id=rs.getString("product_id");
pro.setProduct_id(product_id);
String product_name=rs.getString("product_name");
pro.setProduct_name(product_name);
float product_price=rs.getFloat("product_price");
pro.setProduct_price(product_price);
list.add(pro);
}
rs.close();
stmt.close();
this.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Product selectone(String id){
Product pro=new Product();
List list=new ArrayList();
try {
Connection conn=this.conn();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from product where product_id='"+id+"'");
if(rs.next()){
pro.setProduct_name(rs.getString(2));
pro.setProduct_price(rs.getFloat(3));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pro;
}
public void delete(String product_id){
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="delete from product where product_id='"+product_id+"'";
stmt.executeUpdate(sql);
stmt.close();
this.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(Product pro){
String product_name=pro.getProduct_name();
float product_price=pro.getProduct_price();
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="insert into product(product_name,product_price) values('"+product_name+"','"+product_price+"')";
stmt.executeUpdate(sql);
stmt.close();
this.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(Product pro){
String product_id=pro.getProduct_id();
String product_name=pro.getProduct_name();
float product_price=pro.getProduct_price();
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="update product set product_name='"+product_name+"',product_price="+product_price+" where product_id='"+product_id+"'";
stmt.executeUpdate(sql);
stmt.close();
this.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean search(String name){
boolean i=false;
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="select * from product where product_name='"+name+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
i=true;
}
rs.close();
stmt.close();
this.conn();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public List getnameList(String name){
List list=new ArrayList();
try {
Connection conn=this.conn();
Statement stmt=conn.createStatement();
String sql="select * from product where product_name='"+name+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
Product pro=new Product();
String product_id=rs.getString("product_id");
pro.setProduct_id(product_id);
String product_name=rs.getString("product_name");
pro.setProduct_name(product_name);
float product_price=rs.getFloat("product_price");
pro.setProduct_price(product_price);
list.add(pro);
}
rs.close();
stmt.close();
this.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public void close(Connection conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
servlet文件
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
public Servlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String kind=request.getParameter("kind");
//按条件查询
if(kind.equalsIgnoreCase("select")){
String product_name=Change.changetoString(request.getParameter("name"));
ProductDao pdao=new ProductDao();
boolean i=pdao.search(product_name);
if(i==false){
response.sendRedirect("../fit/notfind.jsp");
}else{
request.setAttribute("name123",product_name);
RequestDispatcher rd = request.getRequestDispatcher("../fit/selectname.jsp");
rd.forward(request,response);
}
}
//插入信息
if(kind.equalsIgnoreCase("insert")){
String product_name =Change.changetoString(request.getParameter("name"));
float product_price = Float.valueOf(request.getParameter("price"));
List list = new ArrayList();
ProductDao pdao = new ProductDao();
list = pdao.getUserList();
Product pro = null;
int i;
for (i = 0; i < list.size(); i++) {
pro = (Product) list.get(i);
if (product_name.equalsIgnoreCase(pro.getProduct_name())) {
response.sendRedirect("../fit/insert.jsp");
break;
}
else if(product_name==null){
response.sendRedirect("../fit/insert.jsp");
}
}
if(i==list.size()){
Product prod=new Product();
prod.setProduct_name(product_name);
prod.setProduct_price(product_price);
pdao.insert(prod);
response.sendRedirect("../fit/select.jsp");
}
}
//更新数据
if(kind.equalsIgnoreCase("update")){
Product pro = new Product();
pro.setProduct_id(request.getParameter("product_id"));
pro.setProduct_name(Change.changetoString(request.getParameter("product_name")));
pro.setProduct_price(Float.valueOf(request.getParameter("product_price")));
ProductDao pdao = new ProductDao();
pdao.update(pro);
response.sendRedirect("../fit/select.jsp");
}
//删除一条数据
if(kind.equalsIgnoreCase("delete")){
String product_id=request.getParameter("product_id");
ProductDao pdao=new ProductDao();
pdao.delete(product_id);
response.sendRedirect("../fit/delete.jsp");
}
}
public void init() throws ServletException {
}
}
删除页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getUserList();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<table width="500">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
Product pro=null;
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
<td width="100">
<input type="hidden" name="kind">
<font size="2" color="blue"><a href="../servlet/Servlet?kind=delete&product_id=<%=pro.getProduct_id() %>">删除</a></font>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
主页框架页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<frameset rows="15%,*">
<frame src="top.html" name="top">
<frameset cols="15%,*">
<frame src="left.jsp" name="left">
<frame src="select.jsp" name="index">
</frameset>
</frameset>
</html>
插入信息页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>insert.jsp</title>
</head>
<body>
<form action="../servlet/Servlet" method="post">
<table>
<tr>
<td>
产品名称:<input type="text" name="name">
</td>
</tr>
<tr>
<td>
产品价格:<input type="text" name="price">
</td>
</tr>
<tr>
<td>
<input type="submit" value="添加">
<input type="hidden" name="kind" value="insert">
</td>
</tr>
</table>
</form>
</body>
</html>
搜索页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="../servlet/Servlet">
产品名称: <input type="text" name="name">
<input type="submit" value="搜索">
<input type="hidden" name="kind" value="select">
</form>
</body>
</html>
显示全部商品信息
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getUserList();
Product pro=null;
%>
<table width="400">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
搜索后得到的页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>selectname.jsp</title>
</head>
<body>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getnameList((String)request.getAttribute("name123"));
Product pro=null;
%>
<table width="400">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
更新页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getUserList();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<table width="500">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
Product pro=null;
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
<td width="100">
<font size="2" color="blue"><a href="update_ope.jsp?product_id=<%=pro.getProduct_id() %>">修改</a></font>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
更新页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>update_ope.jsp</title>
<%
String id=request.getParameter("product_id");
List list=new ArrayList();
ProductDao pdao=new ProductDao();
Product pro = pdao.selectone(id);
String product_name=pro.getProduct_name();
float product_price=pro.getProduct_price();
%>
</head>
<body>
<form action="../servlet/Servlet">
<table>
<tr>
<td>
商品编号:<input type="text" name="product_id" value=<%=id %> readonly>
</td>
</tr>
<tr>
<td>
商品名称:<input type="text" name="product_name" value=<%=product_name %>>
</td>
</tr>
<tr>
<td>
商品价格:<input type="text" name="product_price" value=<%=product_price %> >
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="修改">
<input type="hidden" name="kind" value="update">
</td>
</tr>
</table>
</form>
</body>
</html>
http://blog.sina.com.cn/s/blog_5f6e0a9a0100cv3k.html
转载的,在此,谢谢原作者。
将汉字转为可以识别的光标码
package com;
import java.io.UnsupportedEncodingException;
public class Change {
public static String changetoString(String str){
String returnstr=null;
try {
byte b[]=str.getBytes("ISO-8859-1");
returnstr=new String(b,"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnstr;
}
}
商品类
package com;
public class Product {
private String product_id;
private String product_name;
private float product_price;
public String getProduct_id() {
return product_id;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public float getProduct_price() {
return product_price;
}
public void setProduct_price(float product_price) {
this.product_price = product_price;
}
}
dao处理文件
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class ProductDao {
public Connection conn(){
Connection conn=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=chanpin;user=sa;password=sa";
conn=DriverManager.getConnection(URL);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public List getUserList(){
List list=new ArrayList();
try {
Connection conn=this.conn();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from product");
while(rs.next()){
Product pro=new Product();
String product_id=rs.getString("product_id");
pro.setProduct_id(product_id);
String product_name=rs.getString("product_name");
pro.setProduct_name(product_name);
float product_price=rs.getFloat("product_price");
pro.setProduct_price(product_price);
list.add(pro);
}
rs.close();
stmt.close();
this.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Product selectone(String id){
Product pro=new Product();
List list=new ArrayList();
try {
Connection conn=this.conn();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from product where product_id='"+id+"'");
if(rs.next()){
pro.setProduct_name(rs.getString(2));
pro.setProduct_price(rs.getFloat(3));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pro;
}
public void delete(String product_id){
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="delete from product where product_id='"+product_id+"'";
stmt.executeUpdate(sql);
stmt.close();
this.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(Product pro){
String product_name=pro.getProduct_name();
float product_price=pro.getProduct_price();
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="insert into product(product_name,product_price) values('"+product_name+"','"+product_price+"')";
stmt.executeUpdate(sql);
stmt.close();
this.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(Product pro){
String product_id=pro.getProduct_id();
String product_name=pro.getProduct_name();
float product_price=pro.getProduct_price();
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="update product set product_name='"+product_name+"',product_price="+product_price+" where product_id='"+product_id+"'";
stmt.executeUpdate(sql);
stmt.close();
this.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean search(String name){
boolean i=false;
Connection conn=this.conn();
try {
Statement stmt=conn.createStatement();
String sql="select * from product where product_name='"+name+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
i=true;
}
rs.close();
stmt.close();
this.conn();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public List getnameList(String name){
List list=new ArrayList();
try {
Connection conn=this.conn();
Statement stmt=conn.createStatement();
String sql="select * from product where product_name='"+name+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
Product pro=new Product();
String product_id=rs.getString("product_id");
pro.setProduct_id(product_id);
String product_name=rs.getString("product_name");
pro.setProduct_name(product_name);
float product_price=rs.getFloat("product_price");
pro.setProduct_price(product_price);
list.add(pro);
}
rs.close();
stmt.close();
this.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public void close(Connection conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
servlet文件
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
public Servlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String kind=request.getParameter("kind");
//按条件查询
if(kind.equalsIgnoreCase("select")){
String product_name=Change.changetoString(request.getParameter("name"));
ProductDao pdao=new ProductDao();
boolean i=pdao.search(product_name);
if(i==false){
response.sendRedirect("../fit/notfind.jsp");
}else{
request.setAttribute("name123",product_name);
RequestDispatcher rd = request.getRequestDispatcher("../fit/selectname.jsp");
rd.forward(request,response);
}
}
//插入信息
if(kind.equalsIgnoreCase("insert")){
String product_name =Change.changetoString(request.getParameter("name"));
float product_price = Float.valueOf(request.getParameter("price"));
List list = new ArrayList();
ProductDao pdao = new ProductDao();
list = pdao.getUserList();
Product pro = null;
int i;
for (i = 0; i < list.size(); i++) {
pro = (Product) list.get(i);
if (product_name.equalsIgnoreCase(pro.getProduct_name())) {
response.sendRedirect("../fit/insert.jsp");
break;
}
else if(product_name==null){
response.sendRedirect("../fit/insert.jsp");
}
}
if(i==list.size()){
Product prod=new Product();
prod.setProduct_name(product_name);
prod.setProduct_price(product_price);
pdao.insert(prod);
response.sendRedirect("../fit/select.jsp");
}
}
//更新数据
if(kind.equalsIgnoreCase("update")){
Product pro = new Product();
pro.setProduct_id(request.getParameter("product_id"));
pro.setProduct_name(Change.changetoString(request.getParameter("product_name")));
pro.setProduct_price(Float.valueOf(request.getParameter("product_price")));
ProductDao pdao = new ProductDao();
pdao.update(pro);
response.sendRedirect("../fit/select.jsp");
}
//删除一条数据
if(kind.equalsIgnoreCase("delete")){
String product_id=request.getParameter("product_id");
ProductDao pdao=new ProductDao();
pdao.delete(product_id);
response.sendRedirect("../fit/delete.jsp");
}
}
public void init() throws ServletException {
}
}
删除页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getUserList();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<table width="500">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
Product pro=null;
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
<td width="100">
<input type="hidden" name="kind">
<font size="2" color="blue"><a href="../servlet/Servlet?kind=delete&product_id=<%=pro.getProduct_id() %>">删除</a></font>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
主页框架页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<frameset rows="15%,*">
<frame src="top.html" name="top">
<frameset cols="15%,*">
<frame src="left.jsp" name="left">
<frame src="select.jsp" name="index">
</frameset>
</frameset>
</html>
插入信息页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>insert.jsp</title>
</head>
<body>
<form action="../servlet/Servlet" method="post">
<table>
<tr>
<td>
产品名称:<input type="text" name="name">
</td>
</tr>
<tr>
<td>
产品价格:<input type="text" name="price">
</td>
</tr>
<tr>
<td>
<input type="submit" value="添加">
<input type="hidden" name="kind" value="insert">
</td>
</tr>
</table>
</form>
</body>
</html>
搜索页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="../servlet/Servlet">
产品名称: <input type="text" name="name">
<input type="submit" value="搜索">
<input type="hidden" name="kind" value="select">
</form>
</body>
</html>
显示全部商品信息
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getUserList();
Product pro=null;
%>
<table width="400">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
搜索后得到的页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>selectname.jsp</title>
</head>
<body>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getnameList((String)request.getAttribute("name123"));
Product pro=null;
%>
<table width="400">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
更新页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<%
List list=new ArrayList();
ProductDao pdao=new ProductDao();
list=pdao.getUserList();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<table width="500">
<tr>
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<%
Product pro=null;
for(int i=0;i<list.size();i++)
{
pro=(Product)list.get(i);
%>
<tr>
<td>
<%=pro.getProduct_id() %>
</td>
<td>
<%=pro.getProduct_name() %>
</td>
<td>
<%=pro.getProduct_price() %>
</td>
<td width="100">
<font size="2" color="blue"><a href="update_ope.jsp?product_id=<%=pro.getProduct_id() %>">修改</a></font>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
更新页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.Product" %>
<%@ page import="com.ProductDao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>update_ope.jsp</title>
<%
String id=request.getParameter("product_id");
List list=new ArrayList();
ProductDao pdao=new ProductDao();
Product pro = pdao.selectone(id);
String product_name=pro.getProduct_name();
float product_price=pro.getProduct_price();
%>
</head>
<body>
<form action="../servlet/Servlet">
<table>
<tr>
<td>
商品编号:<input type="text" name="product_id" value=<%=id %> readonly>
</td>
</tr>
<tr>
<td>
商品名称:<input type="text" name="product_name" value=<%=product_name %>>
</td>
</tr>
<tr>
<td>
商品价格:<input type="text" name="product_price" value=<%=product_price %> >
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="修改">
<input type="hidden" name="kind" value="update">
</td>
</tr>
</table>
</form>
</body>
</html>