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);
}
}