<%@ 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方式。
看了下,代码没问题,检查下是否是数据库的原因。把插入数据库的代码提取出来,写一个测试类,单独测试。
比如这个
package org.querydemo
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement
public class QueryDemo {
/**
* @param args
*/
//驱动程序就是之前在classpath中配置的JDBC的驱动程序的JAR 包中
public static final String DBDRIVER = "com.mysql.jdbc.Driver"
//连接地址是由各个数据库生产商单独提供的,所以需要单独记住
public static final String DBURL = "jdbc:mysql://localhost:3306/test"
//连接数据库的用户名
public static final String DBUSER = "root"
//连接数据库的密码
public static final String DBPASS = ""
public static void main(String[] args) throws Exception {
Connection con = null//表示数据库的连接对象
Statement stmt = null //表示数据库的更新 *** 作
ResultSet result = null//表示接收数据库的查询结果
Class.forName(DBDRIVER)//1、使用CLASS 类加载驱动程序
con = DriverManager.getConnection(DBURL,DBUSER,DBPASS)//2、连接数据库
stmt = con.createStatement()//3、Statement 接口需要通过Connection 接口进行实例化 *** 作
result = stmt.executeQuery("select name,age,address from java_study.person")//执行SQL 语句,查询数据库
while (result.next()){
String name = result.getString("name")
int age = result.getInt("age")
String address = result.getString("address")
System.out.println(name+age+address)
}
result.close()
con.close()// 4、关闭数据库
}
}
要实现将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方法,执行 *** 作,最后通过转发跳转会原来的界面。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)