Servlet+Javabean+Html实现简单的查询.删除.修改.添加四个功能

November 15, 2015

1.

1.1 首先创建一个单列,供外界实用:

package cn.com.yong.Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
    private String url="jdbc:mysql://localhost:3306/guimei";
    private String user="root";
    private String password="666888";
    private Connection con=null;
    //构造方法私有化
    private DBConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //创建一个私有化对象
    private static DBConnection jdbc=new DBConnection();
    //实例
    public static DBConnection getDBConnectionInstance(){
        return jdbc;
    }
    public Connection getDBConnection() {
        return con;
    }
}

 

1.2 创建一个实体类(这里我们以订单明细为列)

package cn.com.yong.Pojo;
import java.sql.Date;

public class Order_table {
    private Integer order_id;//订单号
    private String order_address;//订单发货地址
    private Integer order_user_id;//订单人的id
    private Integer order_sum_price;//订单总金额
    private Date order_date;//下单日期:符合2004-12-12格式
    private Integer order_status;//订单状态:1表示订单“待审核”:表示您的订单等待审核中。2表示订单“已确认”:表示您的订单已审核通过。3表示订单“备货中”:表示您的订单已打印,我们正在为您备货。4表示订单“已发货”:表示您的订单已出仓库,您可在网上跟踪配送情况。5表示订单“已完成”:表示您的订单已完成签收。值只能取1-5。
    private Integer payment_id;//支付明细表
    public Order_table(){}
    public Order_table(Integer order_id, String order_address,
            Integer order_user_id, Integer order_sum_price, Date order_date,
            Integer order_status, Integer payment_id) {
        this.order_id = order_id;
        this.order_address = order_address;
        this.order_user_id = order_user_id;
        this.order_sum_price = order_sum_price;
        this.order_date = order_date;
        this.order_status = order_status;
        this.payment_id = payment_id;
    }
    public Integer getOrder_id() {
        return order_id;
    }
    public void setOrder_id(Integer order_id) {
        this.order_id = order_id;
    }
    public String getOrder_address() {
        return order_address;
    }
    public void setOrder_address(String order_address) {
        this.order_address = order_address;
    }
    public Integer getOrder_user_id() {
        return order_user_id;
    }
    public void setOrder_user_id(Integer order_user_id) {
        this.order_user_id = order_user_id;
    }
    public Integer getOrder_sum_price() {
        return order_sum_price;
    }
    public void setOrder_sum_price(Integer order_sum_price) {
        this.order_sum_price = order_sum_price;
    }
    public Date getOrder_date() {
        return order_date;
    }
    public void setOrder_date(Date order_date) {
        this.order_date = order_date;
    }
    public Integer getOrder_status() {
        return order_status;
    }
    public void setOrder_status(Integer order_status) {
        this.order_status = order_status;
    }
    public Integer getPayment_id() {
        return payment_id;
    }
    public void setPayment_id(Integer payment_id) {
        this.payment_id = payment_id;
    }
}

 

1.3 创建接口,添加我们需要的方法

package cn.com.yong.Dao;

import java.util.List;

import cn.com.yong.Pojo.Order_table;

public interface Order_tableDao {
    public boolean addOrder_table(Order_table ot);
    public boolean deleteOrder_table(int id);
    public List<Order_table> selectOrder_table();
    public boolean updateOrder_table(Order_table ot);
}

 


1.4 这里实现我们的接口,添加功能

package cn.com.yong.DaoImplement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.com.yong.Dao.Order_tableDao;
import cn.com.yong.Pojo.Order_table;
import cn.com.yong.Utils.DBConnection;

public class Order_tableDaoImplement implements Order_tableDao{
    
    boolean bn=false;
    Statement sta=null;
    ResultSet rs=null;
    PreparedStatement ps=null;
    Connection con=null;

    public boolean addOrder_table(Order_table ot) {
        con=DBConnection.getDBConnectionInstance().getDBConnection();
        String sql="insert into order_table value(?,?,?,?,?,?,?)";
        try {
            ps=con.prepareStatement(sql);
            ps.setInt(1, ot.getOrder_id().intValue());
            ps.setString(2, ot.getOrder_address());
            ps.setInt(3, ot.getOrder_user_id().intValue());
            ps.setInt(4, ot.getOrder_sum_price().intValue());
            ps.setDate(5,ot.getOrder_date());
            ps.setInt(6, ot.getOrder_status().intValue());
            ps.setInt(7, ot.getPayment_id().intValue());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }

    public boolean deleteOrder_table(int id) {
        con=DBConnection.getDBConnectionInstance().getDBConnection();
        String sql="delete from order_table where order_id=?";
        try {
            ps=con.prepareStatement(sql);
            ps.setInt(1, id);
            int i=ps.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }

    public List<Order_table> selectOrder_table() {
        con=DBConnection.getDBConnectionInstance().getDBConnection();
        List<Order_table> list=new ArrayList<Order_table>();
        try {
            sta=con.createStatement();
            String sql="select order_id,order_address,order_user_id,order_sum_price,order_date,order_status,payment_id from order_table";
            rs=sta.executeQuery(sql);
            while(rs.next()){
                Order_table od=new Order_table();
                od.setOrder_id(rs.getInt("order_id"));
                od.setOrder_address(rs.getString("order_address"));
                od.setOrder_user_id(rs.getInt("order_user_id"));
                od.setOrder_sum_price(rs.getInt("order_sum_price"));
                od.setOrder_date(rs.getDate("order_date"));
                od.setOrder_status(rs.getInt("order_status"));
                od.setPayment_id(rs.getInt("payment_id"));
                list.add(od);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public static void main(String[] args) {
        List<Order_table> list=new Order_tableDaoImplement().selectOrder_table();
        System.out.println(list);
    }

    public boolean updateOrder_table(Order_table ot) {
        con=DBConnection.getDBConnectionInstance().getDBConnection();
        String sql="update order_table set order_sum_price=? where order_id=?";
        try {
            ps=con.prepareStatement(sql);
            ps.setInt(1, ot.getOrder_sum_price().intValue());
            ps.setInt(2, ot.getOrder_id().intValue());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }

}

2.

2.1 实现查询功能

2.1.1 Servlet

package cn.com.yong.Servlet;

import java.io.IOException;
import java.util.List;

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

import cn.com.yong.DaoImplement.Order_tableDaoImplement;
import cn.com.yong.Pojo.Order_table;
@WebServlet("/Order_tableSelectAllServlet")
public class Order_tableSelectAllServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;


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

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String url=null;
        Order_tableDaoImplement otd=new Order_tableDaoImplement();
        List<Order_table> list=otd.selectOrder_table();
        
        if(list.size()>0){
            url="Order_tableSelectAll.jsp";
            request.setAttribute("lit", list);
        }else{
            url="loser.jsp";
        }
        request.getRequestDispatcher(url).forward(request, response);
    }

}

2.1.2 jsp页面

<%@page import="cn.com.yong.Pojo.Order_table"%>
<%@page import="cn.com.yong.DaoImplement.Order_tableDaoImplement"%>
<%@ 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 'Order_tableSelectAll.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>
    <table border="1" cellpadding="1" cellspacing="1" align="center" width="80%">
        <tr>
            <th>订单号</th>
            <th>订单发货地址</th>
            <th>订单id</th>
            <th>订单总金额</th>
            <th>下单日期</th>
            <th>订单状态</th>
            <th>支付明细</th>
            <th>操作</th>
        </tr>
        <%
        
        List<Order_table> list=(List<Order_table>)request.getAttribute("lit");
        for(Order_table ot:list){%>
            <tr>
            <td><%=ot.getOrder_id()%></td>
            <td><%=ot.getOrder_address()%></td>
            <td><%=ot.getOrder_user_id()%></td>
            <td><%=ot.getOrder_sum_price()%></td>
            <td><%=ot.getOrder_date()%></td>
            <td><%=ot.getOrder_status()%></td>
            <td><%=ot.getPayment_id()%></td>
            <td><a href="Order_tableDelServlet?order_id=<%=ot.getOrder_id()%>">删除</a>||
            <a href="Order_tableUpdateServlet?order_id=<%=ot.getOrder_id()%>">修改</a></td>
            </tr>
        <%} %>
        
        <h3><a href="Order_tableAdd.jsp">添加</a></h3>
    </table>
  </body>
</html>

2.2 实现删除功能

2.2.1 Servlet界面

package cn.com.yong.Servlet;

import java.io.IOException;

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

import cn.com.yong.Dao.Order_tableDao;
import cn.com.yong.DaoImplement.Order_tableDaoImplement;
@WebServlet("/Order_tableDelServlet")
public class Order_tableDelServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String url=null;
        int id=Integer.parseInt(request.getParameter("order_id"));
        
        Order_tableDao otd=new Order_tableDaoImplement();
        boolean bn=otd.deleteOrder_table(id);
       System.out.println(bn);
        if(bn){
            url="Order_tableSelectAllServlet";
        }else{
            url="loser.jsp";
        }
        response.sendRedirect(url);
    }

}

2.3 实现修改功能

2.3.1 Jsp

 1 <%@page import="cn.com.yong.Pojo.Order_table"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'Order_tableUpdate.jsp' starting page</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 
24   </head>
25   
26   <body>
27       <%Order_table ot=(Order_table)request.getAttribute("ot"); %>
28     <form action="Order_tableUpdateServlet2" method="post">
29         <p>订单号:<input name="order_id" type="text" readonly="readonly" value=<%=ot.getOrder_id()%>></p>
30         <p>订单发货地址:<input name="order_address" type="text" value=<%=ot.getOrder_address()%>></p>
31         <p>订单人的id:<input name="order_user_id" type="text" value=<%=ot.getOrder_user_id()%>></p>
32         <p>订单金额:<input name="order_sum_price" type="text" value=<%=ot.getOrder_sum_price()%>></p>
33         <p>下单日期:<input name="order_date" type="text" value=<%=ot.getOrder_date()%>></p>
34         <p>订单状态:<input name="order_status" type="text" value=<%=ot.getOrder_status()%>></p>
35         <p>支付明细表:<input name="payment_id" type="text" value=<%=ot.getPayment_id()%>></p>
36         <p><input type="submit" value="修改"/></p>
37     </form>
38   </body>
39 </html>

2.3.2 Servlet

 1 import java.io.IOException;
 2 import java.sql.Date;
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import cn.com.yong.DaoImplement.Order_tableDaoImplement;
13 import cn.com.yong.Pojo.Order_table;
14 @WebServlet("/Order_tableUpdateServlet2")
15 public class Order_tableUpdateServlet2 extends HttpServlet {
16 
17     private static final long serialVersionUID = 1L;
18 
19     public void doGet(HttpServletRequest request, HttpServletResponse response)
20             throws ServletException, IOException {
21         doPost(request, response);
22     }
23 
24     public void doPost(HttpServletRequest request, HttpServletResponse response)
25             throws ServletException, IOException {
26         Order_tableDaoImplement otd=new Order_tableDaoImplement();
27         Order_table ot=new Order_table();
28         
29         int order_id=Integer.parseInt(request.getParameter("order_id"));
30         String order_address=request.getParameter("order_address");
31         int order_user_id=Integer.parseInt(request.getParameter("order_user_id"));
32         int order_sum_price=Integer.parseInt(request.getParameter("order_sum_price"));
33         Date order_date=null;
34         try {
35             order_date=new Date(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("order_date")).getTime());
36         } catch (ParseException e) {
37             e.printStackTrace();
38         }
39         int order_status=Integer.parseInt(request.getParameter("order_status"));
40         int payment_id=Integer.parseInt(request.getParameter("payment_id"));
41         
42         ot.setOrder_id(order_id);
43         ot.setOrder_address(order_address);
44         ot.setOrder_user_id(order_user_id);
45         ot.setOrder_sum_price(order_sum_price);
46         ot.setOrder_date(order_date);
47         ot.setOrder_status(order_status);
48         ot.setPayment_id(payment_id);
49         
50         boolean bn=otd.updateOrder_table(ot);
51         String url=null;
52         if(bn){
53             url="Order_tableSelectAllServlet";
54         }else{
55             url="loser.jsp";
56         }
57         response.sendRedirect(url);
58     }
59 
60 }

2.4 添加功能

2.4.1 Html(直接用表单提交给Servlet,不需要转发给jsp)

 1  <head>
 2     <title>Order_tableAdd.html</title>
 3     
 4     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 5     <meta http-equiv="description" content="this is my page">
 6     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 7     
 8     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
 9 
10   </head>
11   
12   <body>
13     <form action="Order_tableAddServlet" method="post">
14         <p>订单号:<input name="order_id" type="text"></p>
15         <p>订单发货地址:<input name="order_address" type="text" ></p>
16         <p>订单人的id:<input name="order_user_id" type="text" ></p>
17         <p>订单金额:<input name="order_sum_price" type="text" ></p>
18         <p>下单日期:<input name="order_date" type="text" ></p>
19         <p>订单状态:<input name="order_status" type="text" ></p>
20         <p>支付明细表:<input name="payment_id" type="text"></p>
21         <p><input type="submit" value="添加"/></p>
22     </form>
23   </body>

2.4.2 Servlet

 1 import java.io.IOException;
 2 import java.sql.Date;
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import cn.com.yong.DaoImplement.Order_tableDaoImplement;
13 import cn.com.yong.Pojo.Order_table;
14 @WebServlet("/Order_tableAddServlet")
15 public class Order_tableAddServlet extends HttpServlet {
16 
17     private static final long serialVersionUID = 1L;
18 
19     public void doGet(HttpServletRequest request, HttpServletResponse response)
20             throws ServletException, IOException {
21         doPost(request, response);
22     }
23 
24     public void doPost(HttpServletRequest request, HttpServletResponse response)
25             throws ServletException, IOException {
26         Order_tableDaoImplement otd=new Order_tableDaoImplement();
27         Order_table ot=new Order_table();
28         
29         int order_id=Integer.parseInt(request.getParameter("order_id"));
30         String order_address=request.getParameter("order_address");
31         int order_user_id=Integer.parseInt(request.getParameter("order_user_id"));
32         int order_sum_price=Integer.parseInt(request.getParameter("order_sum_price"));
33         Date order_date=null;
34         try {
35             order_date=new Date(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("order_date")).getTime());
36         } catch (ParseException e) {
37             e.printStackTrace();
38         }
39         int order_status=Integer.parseInt(request.getParameter("order_status"));
40         int payment_id=Integer.parseInt(request.getParameter("payment_id"));
41         
42         ot.setOrder_id(order_id);
43         ot.setOrder_address(order_address);
44         ot.setOrder_user_id(order_user_id);
45         ot.setOrder_sum_price(order_sum_price);
46         ot.setOrder_date(order_date);
47         ot.setOrder_status(order_status);
48         ot.setPayment_id(payment_id);
49         
50         
51         boolean bn=otd.addOrder_table(ot);
52         String url=null;
53         if(bn){
54             url="Order_tableSelectAllServlet";
55         }else{
56             url="loser.jsp";
57         }
58         response.sendRedirect(url);
59     }
60 
61 }

注:每创建一个Servlet对象,需要配置文件,如2.4.2十四行所示,不要忘记