html网站表单内容怎么提交到数据库? |
您所在的位置:网站首页 › html表单提交到servlet › html网站表单内容怎么提交到数据库? |
1:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data) 2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。 具体示例: (1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。 (2)其次创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。 (3)接着当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过 $_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。 你好!要提交数据,最好用表单(form),简单明了快捷,然后在form里用input name="submit" type="submit" value="提交" /这个按钮就可以提交到你要处理数据的页面了!设置表单是传输方式post或者get,在你要处理的页面就可以得到这个表单是数据 要实现将jsp中数据添加到数据库并刷新页面可以使用servlet来做中间件,进行数据库的插入 *** 作。 具体示例代码如下: jsp页面: %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" html head /head body form action="/demoServlet" method="post" input type="text" name="num"/br/ input type="text" name="name"/br/ input type="submit" value="提交"/ /form /body /html servlet类: public class DemoServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null PreparedStatement pstmt = null String num = request.getParameter("num") String name = request.getParameter("name") try{ String sql="insert into student values(?,?)" // conn=jdbcTool.getConnection()//获取连接(工具类) pstmt=conn.prepareStatement(sql) pstmt.setString(1,num) pstmt.setString(2,name) pstmt.executeUpdate()//执行插入 } catch(Exception e ){ System.out.println(e.toString()) }finally{ jdbcTool.free(null, pstmt, conn)//关闭连接(工具类) } request.getRequestDispatcher("/demo.jsp").forward(request, response)//重新跳转到本页面(刷新页面) } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response) } } 点击提交按钮后,表单提交,调用doPost方法,执行 *** 作,最后通过转发跳转会原来的界面。 欢迎分享,转载请注明来源:内存溢出 原文地址:https://outofmemory.cn/sjk/9241609.html |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |