<%@ 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方式。
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*,java.sql.SQLException" %><%@ page contentType="text/htmlcharset=gb2312"%>
<html>
<body>
<%
//把经常需要修改的数据放在最上面,以方便修改
String username="root"//数据库用户名
String password="root"//数据库密码
//jsp程序中半数以上的Exception是NonePointerException,在声明变量时赋予一个初始值,能缩短调试时间
ResultSet rs=null//数据库查询结果集
Connection conn=null
Statement stmt=null
//注册驱动程序
try
{
Class.forName("org.gjt.mm.mysql.Driver")
}catch(java.lang.ClassNotFoundException e)
{
System.err.println("Driver Error"+e.getMessage())
}
//连接数据库并创建Statement对象
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk"
try
{
conn=DriverManager.getConnection(url,username,password)
stmt=(Statement)conn.createStatement()
}catch(Exception e)
{
System.err.println("数据库连接错误:"+e.getMessage())
}
//通过Statement执行SQL语句来获取查询结果
try
{
rs=(ResultSet)stmt.executeQuery("select * from userinfo")
}catch(SQLException ex)
{
System.err.println("数据库查错误:"+ex)
}
%>
<table width=85% border=1>
<tr>
<td>编号</td>
<td>真实姓名</td>
<td>电话号码</td>
<td>邮件地址</td>
</tr>
<%
//利用while循环输出各条记录
while(rs.next())
{
%>
<tr>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("username")%></td>
<td><%=rs.getString("phone")%></td>
<td><%=rs.getString("email")%></td>
</tr>
<%
}
%>
</table>
<%
rs.close()
stmt.close()
conn.close()
%>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)