本文介绍: HTML页面输入参数后端如何才能接收到或者是如何写入数据库的两种方式1,form表单,2使用tabl表在利用Ajax传送

1,使用 form表单形式

直接上干货
1.创建springboot工程
2,添加static文件静态的HTML
在这里插入图片描述

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"&gt;
    <title&gt;add_medicine_name</title&gt;
</head>
<body>
<form action="add_medicine_name.do" method="post">
    <input type="text" name="name_medicine" placeholder="输入中药名:">
    <textarea type="text" name="efficacy" placeholder="输入药效:"></textarea>
    <textarea type="text" name="can_match" placeholder="输入能够搭配的药名:"></textarea>
    <textarea type="text" name="no_collocation" placeholder="输入不能够搭配的药名:"></textarea>
    <textarea type="text" name="common_formula_and_effect" placeholder="输入当前药物能搭配的药方和疗效:"></textarea>
    <textarea type="text" name="cottoms_desc" placeholder="输入病例情况:"></textarea>
    <button>提交</button>
</form>
</body>
</html>

3,写好控制器连接数据库这里就不讲JDBC的连接过程,还有创建数据的过程)

package com.walnut.medicine_manage_v01;

import com.mysql.cj.jdbc.MysqlDataSource;
import com.sun.org.glassfish.gmbal.ParameterNames;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
使用重定向
使用回应体
@author hutao
@time 2023 03 14

*/

@Controller
public class MedicineController {
    //连接数据库
    //1获取数据
    DataSource dataSource=new MysqlDataSource();

    @PostMapping("add_medicine_name.do")
    public String getMedicineName(//获取表中输入放入参数
            @RequestParam("name_medicine")String name_medicine,
            @RequestParam("efficacy")String efficacy,
            @RequestParam("can_match")String can_match,
            @RequestParam("no_collocation")String no_collocation,
            @RequestParam("common_formula_and_effect")String common_formula_and_effect,
            @RequestParam("cottoms_desc")String cottoms_desc
    ) throws SQLException {
        System.out.println("输入中药名::"+name_medicine);
        System.out.println("输入药效:"+efficacy);
        System.out.println("输入能够搭配的药名:"+can_match);
        System.out.println("输入不能够搭配的药名:"+no_collocation);
        System.out.println("输入当前药物能搭配的药方和疗效:"+common_formula_and_effect);
        System.out.println("输入病例情况:"+cottoms_desc);


        //2连接数据库
        try(Connection connection=dataSource.getConnection("root","H@13984994357t")){
            String sql="insert into `formula_of_traditional_chinese_medicine`.`species`(name_medicine,efficacy," +
                    "can_match,no_collocation,common_formula_and_effect,cottoms_desc) values(?,?,?,?,?,?)";
            //执行语句
            try(PreparedStatement ps=connection.prepareStatement(sql)){
                ps.setString(1,name_medicine);
                ps.setString(2,efficacy);
                ps.setString(3,can_match);
                ps.setString(4,no_collocation);
                ps.setString(5,common_formula_and_effect);
                ps.setString(6,cottoms_desc);
                ps.executeUpdate();

            }
        }
        catch (SQLException err){
            System.out.println("添加中药失败(sql语句出错)!!!检查后重新输入");
            err.printStackTrace(System.out);
            return "redirect:/add_medicine_name.html";
        }
        if(name_medicine==null){
            //如果为空那么就添加失败
            System.out.println("添加中药名失败");
            return "redirect:/add_medicine_name.html";
        }else {
            System.out.println("添加中医药名成功");
            return "<a href='add_medicine_name.html'>继续添加</a>  <a href='query_medicine_name.html'>现在查询</a>";
        }
    }
}

4,测试

启动工程
在这里插入图片描述

启动成功
在这里插入图片描述

在这里插入图片描述
http://127.0.0.1:8080/add_medicine_name.html
在这里插入图片描述
在这里插入图片描述
5,查看数据库

SELECT * FROM species
在这里插入图片描述
添加成功

2,使用table后端采用Ajax接受

原文地址:https://blog.csdn.net/m0_46427670/article/details/129790346

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18311.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注