jsp查询修改

也是只帖代码。。。。不讲解。

1、search.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'find.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="goodsServlet">
        商品名:<input type="text" name="goodsName"/><br/>
        商品类型:<input type="text" name="goodsType"/><br/>
        <input type="hidden" value="findAll" name="status"/>
        <input type="submit" value="查询"/>
    </form>
  </body>
</html>

2、show.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'show.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    
    <script type="text/javascript">
        function delGoods(no){
            window.location.href="goodsServlet?no="+no+"&status=delete&goodsName=<%=request.getParameter("goodsName")%>&goodsType=<%=request.getParameter("goodsType")%>";
        }
        
        function editGoods(no){
            window.location.href="goodsServlet?no="+no+"&status=edit&goodsName=<%=request.getParameter("goodsName")%>&goodsType=<%=request.getParameter("goodsType")%>";
        }
    </script>

  </head>
  
  <body>
    <table border="1" cellpadding="0" cellspacing="0" width="600">
        <tr>
            <td>商品编号</td>
            <td>商品名</td>
            <td>生产日期</td>
            <td>地址</td>
            <td>商品类型</td>
            <td>操作</td>
        </tr>
        
        <%
            ResultSet rs = (ResultSet)request.getAttribute("rs");
            while(rs.next()){
                %>
                <tr>
                    <td><%=rs.getString("no") %></td>
                    <td><%=rs.getString("name") %></td>
                    <td><%=rs.getString("time") %></td>
                    <td><%=rs.getString("address") %></td>
                    <td><%=rs.getString("type") %></td>
                    <td>
                        <input type="button" value="删除" onclick="delGoods('<%=rs.getString("no") %>')"/>
                        <input type="button" value="修改" onclick="editGoods('<%=rs.getString("no") %>')"/>
                    </td>
                </tr>
                <%
            }
         %>
    </table>
  </body>
</html>

3、showEdit.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'showEdit.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script type="text/javascript">
        
        function editGoodsOK(no){
            
            document.getElementById("no").value=no;
            document.getElementById("form").submit();
            
        }
    </script>

  </head>
  
  <body>
  <form action="goodsServlet" id="form">
    <table border="1" cellpadding="0" cellspacing="0" width="500">
        <tr>
            <td>商品编号</td>
            <td>商品名</td>
            <td>生产日期</td>
            <td>地址</td>
            <td>商品类型</td>
            <td>操作</td>
        </tr>
        
        <%
            ResultSet rs = (ResultSet)request.getAttribute("rs");
            while(rs.next()){
                %>
                <tr>
                    <td><input type="text" name="no" value="<%=rs.getString("no") %>" readonly="readonly"/></td>
                    <td><input type="text" name="name" value="<%=rs.getString("name") %>"/></td>
                    <td><input type="text" name="time" value="<%=rs.getString("time") %>"/></td>
                    <td><input type="text" name="address" value="<%=rs.getString("address") %>"/></td>
                    <td><input type="text" name="type" value="<%=rs.getString("type") %>"/></td>
                    <td>
                        <input type="button" value="确认修改" onclick="editGoodsOK('<%=rs.getString("no") %>')"/>
                    </td>
                </tr>
                <%
            }
         %>
         <input type="hidden" name="no" id="no"/><br/>
         <input type="hidden" name="status" value="editOK"/><br/>
         <input type="hidden" name="goodsName" value="<%=request.getParameter("goodsName")%>"/><br/>
         <input type="hidden" name="goodsType" value="<%=request.getParameter("goodsType")%>"/><br/>
    </table>
   </form>
  </body>
</html>

4、DBHelper类

package com.cn.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * ��ȡ���ݿ���������Ӷ���?
 * �ر����ݿ�����ĸ������?
 * @author ������
 *
 */
public class DBHelper {
    private static final String className = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/jsp?characterEncoding=utf8&useSSL=true";
    private static final String uname = "root";
    private static final String upass = "**********";
    
    /**
     * ��ȡ���ݿ����Ӷ���ķ���?
     */
    public static Connection getConn(){
        Connection conn = null;
        try{
            Class.forName(className);
            conn = DriverManager.getConnection(url,uname, upass);
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * �ر����ݿ����Ӷ���
     */
    public static void closeConn(Connection conn){
        try{
            if(conn!=null){
                conn.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * �ر����ݿ��������?
     */
    public static void closeStmt(Statement stmt){
        try{
            if(stmt!=null){
                stmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * �ر����ݿ��������?
     */
    public static void closePstmt(PreparedStatement pstmt){
        try{
            if(pstmt!=null){
                pstmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * �ر����ݿ��������?
     */
    public static void closeRs(ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

5、GoodsServlet

package com.cn.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.tools.DBHelper;

public class GoodsServlet extends HttpServlet {
    
    private DBHelper db = new DBHelper();
    private Connection conn = db.getConn();
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String status = request.getParameter("status");
        
        if(status.equals("findAll")){
            this.findAll(request, response);
        }else if(status.equals("delete")){
            this.delete(request, response);
        }else if(status.equals("edit")){
            this.edit(request, response);
        }else if(status.equals("editOK")){
            this.editOK(request, response);
        }
        
    }
    
    /**
     * 查询商品
     */
    
    public void findAll(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        
        String goodsName = request.getParameter("goodsName");
        String goodsType = request.getParameter("goodsType");
        
        ResultSet rs = null;
        try {
            Statement st = conn.createStatement();
            
            String sql = "select * from goods t where 1=1  ";
            
            if(goodsName!=null||!goodsName.equals("")){
                sql += " and t.name like '%"+goodsName+"%'";
            }
            if(goodsType!=null||!goodsType.equals("")){
                sql += " and t.type like '%"+goodsType+"%'";
            }
            
            rs = st.executeQuery(sql);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        request.setAttribute("rs", rs);
        request.getRequestDispatcher("show.jsp").forward(request, response);
    }
    
    /**
     * 删除商品
     */
    public void delete(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String no = request.getParameter("no");
        try {
            Statement st = conn.createStatement();
            st.execute("delete from goods where no='"+no+"'");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.findAll(request, response);
    }
    
    /**
     * 修改数据展示
     */
    public void edit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String no = request.getParameter("no");
        ResultSet rs = null;
        try {
            Statement st = conn.createStatement();
            rs = st.executeQuery("select * from goods where no='"+no+"'");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        request.setAttribute("rs", rs);
        request.getRequestDispatcher("showEdit.jsp").forward(request, response);
    }
    
    /** 
     * 确认修改
     */
    public void editOK(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String no = request.getParameter("no");
        String name = request.getParameter("name");
        String time = request.getParameter("time");
        String address = request.getParameter("address");
        String type = request.getParameter("type");
        
        try {
            Statement st = conn.createStatement();
            st.execute("update goods set name='"+name+"',time='"+time+"',address='"+address+"',type='"+type+"' where no='"+no+"'");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.findAll(request, response);
    }

}

 

转载于:https://www.cnblogs.com/Vito-Yan/p/7147688.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值