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

可以具体点吗?比如需要什么源码和操作?... 可以具体点吗?比如需要什么源码和操作? 展开
 我来答
陈MC乐园
高粉答主

2018-04-12 · 每个回答都超有意思的
知道小有建树答主
回答量:95
采纳率:100%
帮助的人:1.4万
展开全部
获取表单中的信息,然后插入到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方式。
29120406
2010-09-02 · TA获得超过805个赞
知道小有建树答主
回答量:608
采纳率:0%
帮助的人:317万
展开全部
最好写个SERVLET的类,通过获取表单页面的信息,在通过SQL语句保存到数据库里面

参考资料: L

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
25463chen
2010-09-03 · TA获得超过441个赞
知道小有建树答主
回答量:133
采纳率:100%
帮助的人:160万
展开全部
http://zhidao.baidu.com/question/180095404.html
参考我写的这个吧,不想再写第二遍了,应该能看懂的。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
最瑜伽
推荐于2017-11-23 · TA获得超过645个赞
知道小有建树答主
回答量:788
采纳率:0%
帮助的人:171万
展开全部
获取表单中的信息,然后插入到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方式。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式