Notes_Servlet(Servlet_0318)

191835_rtoV_3716995.png

191926_M4bt_3716995.png

191902_s4Xr_3716995.png

删除了zy

191958_bi6n_3716995.png

这是NoteServlet

package org.jsoft.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.jsoft.dao.NotesDAO;
import org.jsoft.vo.NotesVO;

/**
 * Servlet implementation class NotesServlet
 */
public class NotesServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NotesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // 因为超链接a是用get,所以写get里面
        String method = request.getParameter("method");
        if ("del".equals(method)) {
            NotesDAO nd1 = new NotesDAO();
            NotesVO nv1 = new NotesVO();
            String id = request.getParameter("nid");
            int nid = Integer.parseInt(id);
            nv1.setNid(nid);
            nd1.delN(nv1);
            // response.sendRedirect("Select.jsp");
            request.getRequestDispatcher("Select.jsp").forward(request,
                    response);
            // 两种跳转方式都可以跳转
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String method = request.getParameter("method");
        request.setCharacterEncoding("UTF-8"); // 只能用post方式
        // 获取表单数据,调用insert方法
        String ntitle = request.getParameter("ntitle");
        String price = request.getParameter("nprice");
        double nprice = Double.parseDouble(price);
        // 获得当前系统日期
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 把日期转换成我们要的格式
        String sdate = sdf.format(d);
        Date ndate = null;
        try {
            if ("login".equals(method)) {
                ndate = sdf.parse(sdate);
                String nphoto = "images/ty.png";
                // 调用 insert 方法
                NotesDAO nd = new NotesDAO();
                NotesVO nv = new NotesVO();
                nv.setNtitle(ntitle);
                nv.setNdate(ndate);
                nv.setNphoto(nphoto);
                nv.setNprice(nprice);
                nd.insertN(nv);
                request.getRequestDispatcher("Select.jsp").forward(request,response);
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // 把字符串转换成日期
            // 获得图片

        if ("upd".equals(method)) {
            NotesDAO nd2 = new NotesDAO();
            NotesVO nv2 = new NotesVO();
            request.setCharacterEncoding("UTF-8"); // 只能用post方式
            // 获取表单数据,调用insert方法
            String ntitle1 = request.getParameter("ntitle");
            String price1 = request.getParameter("nprice");
            double nprice1 = Double.parseDouble(price1);
            String id = request.getParameter("nid");
            int nid = Integer.parseInt(id);
            nv2.setNid(nid);
            nv2.setNtitle(ntitle1);
            nv2.setNprice(nprice1);
            nd2.updateN(nv2);
            request.getRequestDispatcher("Select.jsp").forward(request,response);
        }
    }

}

这是注册标题打赏页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="NotesServlet?method=login" method="post" >
    标题:<input type="text" name="ntitle"><br>
    赏金:<input type="text" name="nprice"><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

这是更新页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="NotesServlet?method=upd&&nid=<%=request.getParameter("nid") %>" method="post">
 <table>
    
     <tr>
          <td>编号</td> <!-- 不可改变的 -->
         <td>标题</td>
         <td>打赏</td>
     </tr>
        <!--  显示点击修改那 一条信息--> 
     <tr>
     <!-- 
     为什么写这个还乱码
     String ntitle=request.getParameter("ntitle");
     String str=new String(ntitle.getBytes("ISO8859-1"),"UTF-8"); -->
         <td> <input type="text" name="nid" value="<%=request.getParameter("nid") %>" disabled="disabled"></td>
         <td><input type="text" name="ntitle" value="<%=request.getParameter("ntitle") %>"></td>
         <td><input type="text" name="nprice" value="<%=request.getParameter("nprice") %>"></td>
         <td><input type="submit" value="修改"></td>
     </tr>
     
 </table>
 </form>
</body>
</html>

这是查询页面

<%@page import="org.jsoft.vo.NotesVO"%>
<%@page import="java.util.List"%>
<%@page import="org.jsoft.dao.NotesDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" cellspacing="0">
     <tr>
         <td>编号</td>
         <td>标题</td>
         <td>日期</td>
         <td>图片</td>
         <td>打赏</td>
         <td>操作</td>
     </tr>
     <%
         NotesDAO nd =new NotesDAO();
     List<NotesVO> l=nd.getNotes();
     for(NotesVO nv:l){
         
     %>
         <tr>
         <td><%=nv.getNid() %></td>
         <td><%=nv.getNtitle() %></td>
         <td><%=nv.getNdate() %></td>
         <td><img src="<%=nv.getNphoto() %>"/></td>
         <td><%=nv.getNprice() %></td>
         <td><a href="NotesServlet?method=del&&nid=<%=nv.getNid() %>">删除</a></td>
         <td><a href="Update.jsp?nid=<%=nv.getNid() %>&ntitle=<%=nv.getNtitle() %>&nprice=<%=nv.getNprice() %>">修改</a></td>
     </tr>    
     <%    
     }
     %>
     </table>
</body>
</html>

这是dao层

 

package org.jsoft.dao;

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

import org.jsoft.conn.SingleTest;
import org.jsoft.vo.NotesVO;

public class NotesDAO {
    //插入
    
    public boolean insertN(NotesVO nv){
        boolean b=false;
        // 链接
        Connection con=null;
        PreparedStatement pst =null;
        try {
            con =SingleTest.getST().getCon();
        String sql="insert into note(ntitle,ndate,nphoto,nprice) values(?,?,?,?)";
        pst =con.prepareStatement(sql);
        pst.setString(1, nv.getNtitle());
        // 把util 下的 date 转换成 sql下的date
        java.util.Date d=nv.getNdate();
            long dd =d.getTime();
        java.sql.Date sd=new java.sql.Date(dd);
        pst.setDate(2, sd);
        pst.setString(3, nv.getNphoto());
        pst.setDouble(4, nv.getNprice());
        int i =pst.executeUpdate();
        if(i>0){
            b=true;
        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            SingleTest.getST().closeAll(null, pst, con);
        }
        
        return b;
    }
    //查询方法
    public List<NotesVO> getNotes(){
        List<NotesVO> l =new ArrayList<NotesVO>();
        //获得链接
        Connection con=null;
        PreparedStatement pst =null;
        ResultSet rs =null;
        try {
            con =SingleTest.getST().getCon();
        String sql="select * from note";
        // 处理sql
        pst =con.prepareStatement(sql);
        rs =pst.executeQuery();
        //遍历rs
        while(rs.next()){
            NotesVO nv =new NotesVO(); //kong
            nv.setNid(rs.getInt("nid"));
            nv.setNtitle(rs.getString("ntitle"));
            nv.setNdate(rs.getDate("ndate"));
            nv.setNphoto(rs.getString("nphoto"));
            nv.setNprice(rs.getDouble("nprice"));
            l.add(nv);
        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            SingleTest.getST().closeAll(rs, pst, con);
        }
        return l;
    }
    //删除
    public boolean delN(NotesVO nv){
        boolean b=false;
        // 链接
        Connection con=null;
        PreparedStatement pst =null;
        try {
            con =SingleTest.getST().getCon();
        String sql="delete from note where nid =?";
        pst =con.prepareStatement(sql);
        pst.setInt(1, nv.getNid());        
        int i =pst.executeUpdate();
        if(i>0){
            b=true;
        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            SingleTest.getST().closeAll(null, pst, con);
        }
        
        return b;
    }
    
    // 修改
    public boolean updateN(NotesVO nv){
        boolean b=false;
        // 链接
        Connection con=null;
        PreparedStatement pst =null;
        try {
            con =SingleTest.getST().getCon();
        String sql="update note set ntitle=?,nprice=? where nid=?";
        pst =con.prepareStatement(sql);
        pst.setString(1, nv.getNtitle());
        // 把util 下的 date 转换成 sql下的date
        pst.setDouble(2, nv.getNprice());
        pst.setInt(3, nv.getNid());
        int i =pst.executeUpdate();
        if(i>0){
            b=true;
        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            SingleTest.getST().closeAll(null, pst, con);
        }
        
        return b;
    }
}


 

 

转载于:https://my.oschina.net/u/3716995/blog/1648056

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值