JSP+Servlet+Tomcat+Mysql实现注册、登陆、修改、添加、查看、分页实例之----添加

1、新增insert页面

<%--
  Created by IntelliJ IDEA.
  User: lvjun
  Date: 2018-03-23
  Time: 14:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>新增数据</title>
    </head>
    <body>
        <form action="insert" method="post">
            <table border="1">
                <!--
                <tr>
                    <td><input type="hidden" name="id"></td>
                </tr>
                -->
                <tr>
                    <td>标题<input type="text" name="title"></td>
                </tr>
                <tr>
                    <td>作者<input type="text" name="author"></td>
                </tr>
                <tr>
                    <td>时间<input type="text" name="ctime"></td>
                </tr>
                <tr>
                    <td>类型<input type="text" name="type"></td>
                </tr>
                <tr>
                    <td>状态<input type="stat" name="stat"></td>
                </tr>
                <tr>
                    <td>内容<input type="content" name="content"></td>
                </tr>
                <tr>
                    <td align="center"><input type="submit" value="新增"/></td>
                </tr>
                <tr>
                    <td align="center"><a href="/return">返回首页</a></td>
                </tr>
            </table>
        </form>
    </body>
</html>


2、InserServlet.java

package com.servlet.bbs;

import com.dao.BbsDao;
import com.entity.BbsEntity;

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 java.io.IOException;
import java.util.List;

/**
 * 新增
 * Created by lvjun on 2018-03-23.
 */
@WebServlet(name = "InsertServlet")
public class InsertServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //获取参数
        // int id =Integer.parseInt(request.getParameter("id")); //id自增长,无需插入
        String title =request.getParameter("title");
        String author =request.getParameter("author");
        String ctime =request.getParameter("ctime");
        String content =request.getParameter("content");
        int type =Integer.parseInt(request.getParameter("type"));
        int stat =Integer.parseInt(request.getParameter("stat"));
        //传入参数
        BbsEntity bbsEntity = new BbsEntity();
        //bbsEntity.setId(id);
        bbsEntity.setTitle(title);
        bbsEntity.setAuthor(author);
        bbsEntity.setCtime(ctime);
        bbsEntity.setContent(content);
        bbsEntity.setType(type);
        bbsEntity.setStat(stat);
        BbsDao bbsDao =new BbsDao();
        bbsDao.GetInsert(bbsEntity); //调用插入数据方法

        //插入成功,返回首页
        List<BbsEntity> list=new BbsDao().GetPageList(0,10);
        request.getSession().setAttribute("list", list); // 文章内容数据
        int total = new BbsDao().GetTotal();
        request.setAttribute("total", total); //加载总数
        request.getRequestDispatcher("/main.jsp").forward(request, response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面过来的跳转
        request.getRequestDispatcher("/insert.jsp").forward(request, response);
    }
}

3、添加数据库操作

 /**
     *新增数据
     **/
    public  void GetInsert(BbsEntity bbsEntity){
        Dbmanage dbmanage = new Dbmanage();
        Connection conn = null;
        Statement sta = null;
        try {
            conn = dbmanage.initDB();
            sta = conn.createStatement();
            String sql = "INSERT INTO bbs(title,author,ctime,content,type,stat)VALUES('"+bbsEntity.getTitle()+"','"+bbsEntity.getAuthor()+"','"+bbsEntity.getCtime()+"','"+bbsEntity.getContent()+"','"+bbsEntity.getType()+"','"+bbsEntity.getStat()+"')";
            sta.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbmanage.closeDB(sta, conn);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值