如何将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方式。

这个问题好做啊 步骤如下:

1、首先在在mysql中创建好数据库再创建好数据表(如:t_tranction,设好相应的字段)

2、在jsp页面中利用apache的开源项目commons-upload 编写好上传功能(当然相应的servlet、service、dao层这些个你要实现的)

3、处理上传后的文件信息(就是一个批量insert *** 作)

希望对你有所帮助!谢谢!

分为两步:

1. 将excel电子表格文件上传到服务器,使用jsmartupload或者其它框架上传

2. 读取excel数据,并插入到mysql中,可以使用jxl或poi框架进行读取解析,读取完毕批处理保存即可


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

原文地址: http://www.outofmemory.cn/sjk/6822253.html

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

发表评论

登录后才能评论

评论列表(0条)

保存