web + servlet + jdbc mysql 实现简单的表单管理界面

目录

  • 数据库创建
  • 数据库连接
  • servlet创建,这里注意一下我的数据库我自己改了一下名字lhx
  • 网页html
  • 运行
  • 文件目录展示

首先我们准备好开发使用的工具以及配置

idea2020
tomcat8.5 创建javaweb参考idea编译Tomcat详细步骤
IDEA通过JDBC连接数据库请参考jdbc连接数据库
需要登陆注册界面请参考web + servlet + jdbc mysql 实现简单的登陆注册界面

数据库创建

首先创建数据库temp
web + servlet + jdbc mysql 实现简单的表单管理界面_第1张图片
创建新的表单temperature
web + servlet + jdbc mysql 实现简单的表单管理界面_第2张图片
web + servlet + jdbc mysql 实现简单的表单管理界面_第3张图片

或者使用建表语句

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

插入数据
web + servlet + jdbc mysql 实现简单的表单管理界面_第4张图片

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

web + servlet + jdbc mysql 实现简单的表单管理界面_第5张图片

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("");
            p.print("");
            p.print("");
            p.print("");
            p.print("");
            p.print("");
            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("");
                p.print("");
                p.print("");if(rs.getInt("isContacted")==0){   p.print("");}else{  p.print("");}

                p.print("");
                p.print("");
                p.print("");}
            p.print("
序号姓名电话14天内是否有湖北接触史日期体温
"+rs.getInt("id")+""+rs.getString("name")+""+rs.getString("telephone")+" "+rs.getString("data")+""+rs.getString("temp")+"

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

网页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>

web + servlet + jdbc mysql 实现简单的表单管理界面_第6张图片

运行

web + servlet + jdbc mysql 实现简单的表单管理界面_第7张图片

web + servlet + jdbc mysql 实现简单的表单管理界面_第8张图片
web + servlet + jdbc mysql 实现简单的表单管理界面_第9张图片

文件目录展示

web + servlet + jdbc mysql 实现简单的表单管理界面_第10张图片

你可能感兴趣的:(servlet,mysql,前端)