如何将JSP页面中的表单信息保存到Mysql数据库?

如何将JSP页面中的表单信息保存到Mysql数据库?,第1张

获取表单中的信息,然后插入到Mysql中 

<%@ page language="java" contentType="text/html charset=gbk"

    pageEncoding="gbk"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%

int id = Integer.parseInt(request.getParameter("id"))

int rootid = Integer.parseInt(request.getParameter("rootid"))

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gbk">

<title>Replay</title>

</head>

<body>

<form method="post" action="ReplayOK.jsp">

 <input type="hidden" name="id" value="<%=id %>">

 <input type="hidden" name="rootid" value="<%=rootid %>">

<table align="center">

 <tr>

  <td>

   <input type="text" name="title" size="80">

  </td>

 </tr>

 <tr>

  <td>

   <textarea cols="80" rows="20" name="cont"></textarea>

  </td>

 </tr>

 <tr>

  <td>

   <input type="submit" value="提交">

  </td>

 </tr>

</table>

</form>

</body>

</html>

---------------------------------------------------------------

下面接收上面表单中传过来的信息,并插入到mysql中

<%@ page language="java" contentType="text/html charset=gbk"

    pageEncoding="gbk"%>

<%@ page import="java.sql.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%

request.setCharacterEncoding("GBK")

int id = Integer.parseInt(request.getParameter("id"))

int rootid = Integer.parseInt(request.getParameter("rootid"))

String title = request.getParameter("title")

String cont = request.getParameter("cont").replaceAll("\n","<br/>")

Connection conn = null

Statement st = null

Class.forName("com.mysql.jdbc.Driver")

conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=690115399")

st = conn.createStatement()

conn.setAutoCommit(false)

String sql = "insert into article values(null,?,?,?,?,now(),0)"

PreparedStatement pstmt = conn.prepareStatement(sql)

pstmt.setInt(1,id)

pstmt.setInt(2,rootid)

pstmt.setString(3,title)

pstmt.setString(4,cont)

pstmt.executeUpdate()

st.executeUpdate("update article set isleaf = 1 where id = " + id)

conn.commit()

conn.setAutoCommit(true)

st.close()

pstmt.close()

conn.close()

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gbk">

<title>Insert title here</title>

</head>

<body>

<%response.sendRedirect("ShowArticleTree.jsp") %>

</body>

</html>

当然最好的方法还是应该用jsp + JavaBean方式。

 失踪之后几乎过了五个月,乌苏娜回来了。她显得异常兴奋;有点返老还童,穿着村里人谁也没有穿过的新式衣服。霍·阿·布恩蒂亚高兴得差点儿发了疯,“原来如此!正象我预料的!”他叫了起来。这是真的,因为待在试验室里进行物质试验的长时间中,他曾在内心深处祈求上帝,他所期待的奇迹不是发现点金石,也不是哈口气让金属具有生命,更不是发明一种办法,以便把金子变成房锁和窗子的铰链,而是刚刚发生的事--乌苏娜的归来。但她并没有跟他一起发狂地高兴。她照旧给了丈夫一个乐吻,仿佛他俩不过一小时以前才见过面似的。说道:

“到门外去看看吧!”

给你个思路吧,前台表单 form中写好提交的地址(jsp本身或者servlet或其他控制器);

控制器中,接收处理提交过来的字符串,然后用 按照连接数据库,插入数据表的顺序写入sql数据,

说白了就是用java *** 作插入性质的sql语句。

新学java吗,建议提问时应该把问题说清楚,什么样的题目,是问答题还是实际的作业,如果是后者更应该说清楚,因为jsp插入数据库的方式很多种,回答的人不一定能满足你的要求,所以你百度一下jsp插入数据库就有简单的实例可以找到并运用了。

纯手打^_^


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9244260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存