javaweb实战,疫情防控数据上报至数据库 html+mysql+servlet

1.前端HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>上报体温</title>
</head>
<body>
	<h3>上报体温</h3>
	<form method="post" action="../add">
		  姓名:<input type ="text" name="name" /><br/><br/>
		  电话:<input type ="text" name="telephone" /><br/><br/>
		  高风险地区接触史:<br/>
		  无<input type ="radio" name="isContacted" value="1" checked/>
		  有<input type ="radio" name="isContacted" value="0" /><br/><br/>
		  体温:<input type ="text" name="temperature" /><br/><br/>`在这里插入代码片`
		<input type="submit" value="  上报  " />
	</form>
</body>
</html>

2.后端逻辑处理,添加数据到数据库。

package test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.Statement;

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

@SuppressWarnings("serial")
public class AddTempServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.service(req, resp);
		req.setCharacterEncoding("UTF-8");
		String name= req.getParameter("name");
		String telephone= req.getParameter("telephone");
		String isContacted= req.getParameter("isContacted");
		String temperature= req.getParameter("temperature");
		int isCon=0;
		
		if(isContacted!=null) {
			isCon=Integer.parseInt(isContacted);	
		}
		float temp=0;
		if(temperature!=null) {
			temp=Float.parseFloat(temperature);
		}
			
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;	
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/temp?characterEncoding=utf8", "root",
					"123456");
			
			String sql = "insert into temperature(name,telephone,isContacted,DATE,temp) values(?,?,?,?,?);";
			pst = con.prepareStatement(sql)	;
			pst.setString(1, name);
			pst.setString(2, telephone);
			pst.setInt(3, isCon);
			pst.setDate(4, new java.sql.Date(new java.util.Date().getTime()));
			pst.setDouble(5, temp);
			
			pst.executeUpdate();
		

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
				}
			}
			if (pst != null) {
				try {
					pst.close();
				} catch (Exception e) {
				}
			}
			if (con != null) {
				try {
					con.close();
				} catch (Exception e) {
				}
			}
		}
		
		
		resp.sendRedirect("list");
	}

}

**3.后端逻辑处理,上报数据之后,返回信息给客户端。**
package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import java.sql.Statement;

@SuppressWarnings("serial")
public class ListTempServlet extends HttpServlet {

	
	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// super.service(arg0, arg1);

	Connection con = null;
	Statement st = null;
	ResultSet rs = null;

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
	//	out.print("<input type='button' value='添加新数据' onclick='location.herf=\"tmp/addTemp.html\"'/");
		out.print("<table border='1px' cellpadding='10' cellspacing='0'>");
		out.print("<tr>");
		out.print("<th> 序号</th>");
		out.print("<th> 姓名</th>");
		out.print("<th> 电话</th>");
		out.print("<th> 是否去过高风险地区</th>");
		out.print("<th> 日期</th>");
		out.print("<th> 体温</th>");
		out.print("<tr>");
		
		

		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/temp?characterEncoding=utf8", "root",
					"123456");
			st = con.createStatement();
			//String sql = "select *from temperature";
			//rs = st.executeQuery(sql);
			
			String sql = "select *from temperature;";
			rs = st.executeQuery(sql);
			//String sql1 = "update temperature set name='王五'  where id='1' ;" ;
			//rs = st.executeQuery(sql1);
			
			while(rs.next()) {
				out.print("<tr>");
				out.print("<td>"+rs.getInt("id")+"</td>");
				out.print("<td>"+rs.getString("name")+"</td>");
				out.print("<td>"+rs.getString("telephone")+"</td>");
				if(rs.getInt("isContacted")==0) {
					out.print("<td style='text-align:center'>否</td>");
				}else {
					out.print("<td style='text-align:center'>是</td>");			
				}
				out.print("<td>"+rs.getDate("date")+"</td>");
				out.print("<td>"+rs.getFloat("temp")+"</td>");
				out.print("</tr>");		
				
				//out.print()
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
				}
			}
			if (st != null) {
				try {
					st.close();
				} catch (Exception e) {
				}
			}
			if (con != null) {
				try {
					con.close();
				} catch (Exception e) {
				}
			}
		}
		//out.print("<a href='C:\Users\Administrator\Desktop\webapp\registe.html'><button>百度</button></a>");
	
		//out.print("<input type='button'  value='添加新数据'  onclick='location.href= 'C:\\Users\\Administrator\\Desktop\\webapp\\registe.html'"/>");
		out.print("</table><br/>");	
		//out.print("<input type='button'    value='添加新数据'   onclick='ocation.href=\" C:\\Users\\Administrator\\Desktop\\webapp\\registe.html \"'/>");
		//out.print("<input type='button' value='  添加新数据  ' "
			//	+ "onclick='location.href=\" C:\\Users\\Administrator\\Desktop\\webapp\\addTemp.html\"' />");	
		
	//	out.print("<input type='button' value='  添加新数据  '  onclick=\' location.href="D:/Users/Administrator/eclipse-workspace/ProjectForMysql/addTemp.html" \'/>");
		
		out.print("<input type='button' value='  添加新数据  ' "
		+ "onclick='location.href=\"temp/Temperature.html \"' />");
	}
}

4.运行效果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值