JavaWeb课堂测试(二)
添加数据
1.创建jsp页面
jsp页面源代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3><center>主界面 </center></h3>
<h1><center>每日打卡</center></h1>
<p><a href="zhuce.jsp">每日打卡</a>
</p>
<p><a href="readNews.jsp">打卡查询</a>
</p>
<p><a href="update.jsp">修改</a>
</p>
<p><a href="manager.jsp">管理待发与已发新闻</a>
</p>
<p><a href="Adm.jsp">管理用户</a>
</p>
<p><a href="login.jsp">用户登录</a>
</p>
</body>
</html>
添加数据页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="End.jsp" method="post">
<h>新闻录制<h>
<dl>
<dt>日期:</dt><input type="text" name="day" /></dd>
<dt>关键字:</dt><input type="text" name="keyword" /></dd>
<dt>每日总结:</dt><input type="text" name="sum" /></dd>
<dt>坚持天数:</dt><input type="text" name="ins" /></dd>
<dt>连续最长天数:</dt><input type="text" name="con /></dd>
<dt><input type="submit" value="提交" /></dd>
</form>
</body>
</html>
写入数据库代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="bean.NewsBean"%>
<%@page import="dao.NewsDao"%>
<%@ page import="java.sql.SQLException" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int i=0;
String day = request.getParameter("day");
String keyword = request.getParameter("keyword");
String sum = request.getParameter("sum");
String ins = request.getParameter("ins");
String con = request.getParameter("con");
String a1=null;
String a2=null;
String a3=null;
String a4=null;
String a5=null;
if(request.getParameter("a1")!=null)
{
a1=request.getParameter("a1")+' ';
}
else a1=" ";
if(request.getParameter("a2")!=null)
{
a2=request.getParameter("a2")+' ';
}
else a2=" ";
if(request.getParameter("a3")!=null)
{
a3=request.getParameter("a3")+' ';
}
else a3=" ";
if(request.getParameter("a4")!=null)
{
a4=request.getParameter("a4")+' ';
}
else a4=" ";
if(request.getParameter("a5")!=null)
{
a5=request.getParameter("a5")+' ';
}
else a5=" ";
NewsBean bean = new NewsBean();
bean.setDay(day);
bean.setKeyword(keyword);
bean.setSum(sum);
bean.setIns(ins);
bean.setCon(con);
NewsDao dao =new NewsDao();
try {
i=dao.add(bean);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
if(i==1)
response.getWriter().append("成功!");
else
response.getWriter().append("错误!");
//response.sendRedirect("screen.jsp");
%>
</body>
</html>