首先我们准备好开发使用的工具以及配置
idea2020
tomcat8.5 创建javaweb参考idea编译Tomcat详细步骤
IDEA通过JDBC连接数据库请参考jdbc连接数据库
需要登陆注册界面请参考web + servlet + jdbc mysql 实现简单的登陆注册界面
或者使用建表语句
CREATE TABLE `temp`.`temperature` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL,
`telephone` CHAR(11) NOT NULL,
`isContacted` TINYINT(1) NOT NULL,
`data` DATE NOT NULL,
`temp` DECIMAL(3) NOT NULL,
PRIMARY KEY (`id`));
insert into temp.temperature value(null,'李原浩','123456','0','2020-2-28','36.5');
insert into temp.temperature value(null,'邢楚','123456','0','2020-2-28','36.2');
insert into temp.temperature value(null,'江哥','123456','0','2020-2-28','36.8');
SELECT * FROM temp.temperature;
连接mysql步骤请看IDEA通过JDBC连接数据库请参考jdbc连接数据库
创建一个测试类
import java.sql.*;
public class test {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
//连接数据库要处理对象我这里是lhx,这个是个数据库大家可以在mysql创建
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/temp", "root", "123456");
PreparedStatement prep = conn.prepareStatement("SELECT * FROM temperature;");
prep.execute();
ResultSet rs = prep.getResultSet();
while (rs.next()) {
System.out.println(rs.getString("id")+
rs.getString("name")+
rs.getString("telephone")+
rs.getString("isContacted")+
rs.getString("data")+
rs.getString("temp"));
}
conn.close();
}
}
连接数据库,传入网页数据代码如下
package Servlet;
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.io.PrintWriter;
import java.sql.*;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setHeader("content-tupe","test/html;GBK");
PrintWriter p = response.getWriter();
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");//注册驱动
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lhx", "root", "123456");
PreparedStatement prep = conn.prepareStatement("SELECT * FROM temperature;");
prep.execute();
rs = prep.getResultSet();
p.print("");
p.print("");
p.print("序号 ");
p.print("姓名 ");
p.print("电话 ");
p.print("14天内是否有湖北接触史 ");
p.print("日期 ");
p.print("体温 ");
p.print(" ");
while (rs.next()) {
//测试
System.out.println(rs.getString("id") +
rs.getString("name") +
rs.getString("telephone") +
rs.getString("isContacted") +
rs.getString("data") +
rs.getString("temp"));
p.print("");
p.print(""+rs.getInt("id")+" ");
p.print(""+rs.getString("name")+" ");
p.print(""+rs.getString("telephone")+" ");
if(rs.getInt("isContacted")==0){ p.print("否 ");}else{ p.print("是 ");}
p.print(""+rs.getString("data")+" ");
p.print(""+rs.getString("temp")+" ");
p.print(" ");
}
p.print("
");
}catch(Exception e){
e.printStackTrace();
}finally {
if (rs!=null){try{rs.close();}catch (SQLException e) {
}}
if (st!=null){try{rs.close();}catch (SQLException e) {
}}
if (con!=null){try{rs.close();}catch (SQLException e) {
}}
}p.print(" +
"οnclick='location.href=\"index.html\"' />");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
配置xml
Servlet
/servlet/Servlet
listServlet
/servlet/ListServlet
Add_Servlet
package temp;
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.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String telephone = request.getParameter("telephone");
String isContacted = request.getParameter("isContacted");
String temperature = request.getParameter("temperature");
int isCon = 0;
if (isContacted != null) {
Integer.parseInt(isContacted);
}
double temp = 0;
if (temperature != null) {
temp = Double.parseDouble(temperature);
}
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
//
//
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lhx", "root", "123456");
String sql = "insert into temperaturevalues(null,?,?,?,?,?)";
pst = connection.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) {
}
}
}
response.sendRedirect("TempServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
配置
servlet
Servlet.Servlet
Add_Servlet
Servlet.Add_Servlet
servlet
/Servlet/Servlet
Add_Servlet
/Servlet/Add_Servlet
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>体温填报title>
head>
<body>
<h3>体温平安报h3>
<form method="post" action="Add_Servlet">
姓名:<input type="text" name="name" /><br/><br/>
电话:<input type="text" name="telephone" /><br/><br/>
最近14天是否有湖北接触史:<br/>
没有:<input type="radio" name="isContacted" value="0" checked/>
有:<input type="radio" name="isContacted" value="1"/><br/><br/>
体温:<input type="text" name="temperature" /><br/><br/>
<input type="submit" value=" 上报 " />
form>
body>
html>