首先我们准备好开发使用的工具以及配置
idea2020
tomcat8.5 创建javaweb参考idea编译Tomcat详细步骤
IDEA通过JDBC连接数据库请参考jdbc连接数据库
需要登陆注册界面请参考web + servlet + jdbc mysql 实现简单的登陆注册界面
数据库创建
首先创建数据库temp
创建新的表单temperature
或者使用建表语句
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();
}
}
servlet创建,这里注意一下我的数据库我自己改了一下名字lhx
连接数据库,传入网页数据代码如下
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("<table border='1px' cellpadding='10' cellspacing='0'>");
p.print("<tr>");
p.print("<th>序号</th>");
p.print("<th>姓名</th>");
p.print("<th>电话</th>");
p.print("<th>14天内是否有湖北接触史</th>");
p.print("<th>日期</th>");
p.print("<th>体温</th>");
p.print("</tr>");
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("<tr>");
p.print("<td>"+rs.getInt("id")+"</td>");
p.print("<td>"+rs.getString("name")+"</td>");
p.print("<td>"+rs.getString("telephone")+"</td>");
if(rs.getInt("isContacted")==0){ p.print("<td style='text-align:center'>否</td>");}else{ p.print("<td style='text-align:center'>是</td>");}
p.print("<td>"+rs.getString("data")+"</td>");
p.print("<td>"+rs.getString("temp")+"</td>");
p.print("</tr>");
}
p.print("</table><br/>");
}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("<input type='button' value=' 添加新数据'" +
"οnclick='location.href=\"index.html\"' />");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
配置xml
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/servlet/Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>listServlet</servlet-name>
<url-pattern>/servlet/ListServlet</url-pattern>
</servlet-mapping>
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-name>servlet</servlet-name>
<servlet-class>Servlet.Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Add_Servlet</servlet-name>
<servlet-class>Servlet.Add_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/Servlet/Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Add_Servlet</servlet-name>
<url-pattern>/Servlet/Add_Servlet</url-pattern>
</servlet-mapping>
网页html
<!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>
运行
文章来源:https://www.toymoban.com/news/detail-400934.html
文件目录展示
文章来源地址https://www.toymoban.com/news/detail-400934.html
到了这里,关于web + servlet + jdbc mysql 实现简单的表单管理界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!