<%@ 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方式。
最简单的JSP页面中的数据库 *** 作方法:<%@ page
language="java"
contentType="text/htmlcharset=UTF-8"
pageEncoding="UTF-8"
%>
<%@page import="java.sql.*"%>
<center>
<H1><font color="blue" size="12">管理中心</font></H1>
<HR />
<table width="80%" border="1">
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>删除</th>
</tr>
<%
// 数据库的名字
String dbName = "zap"
// 登录数据库的用户名
String username = "sa"
// 登录数据库的密码
String password = "123"
// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1"
// 数据库的端口,一般不会修改,默认为1433
int port = 1433
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + "databaseName=" + dbName + "user=" + username
+ "password=" + password
//
//声明需要使用的资源
// 数据库连接,记得用完了一定要关闭
Connection con = null
// Statement 记得用完了一定要关闭
Statement stmt = null
// 结果集,记得用完了一定要关闭
ResultSet rs = null
try {
// 注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
// 获得一个数据库连接
con = DriverManager.getConnection(connectionUrl)
String SQL = "SELECT * from note"
// 创建查询
stmt = con.createStatement()
// 执行查询,拿到结果集
rs = stmt.executeQuery(SQL)
while (rs.next()) {
%>
<tr>
<td>
<%=rs.getInt(1)%>
</td>
<td>
<a href="prepareupdate?ID=<%=rs.getInt("ID")%>" target="_blank"><%=rs.getString(2)%></a>
</td>
<td>
<%=rs.getString(3)%>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<a href="delete?ID=<%=rs.getInt("ID")%>" target="_blank">删除</a>
</td>
</tr>
<%
}
} catch (Exception e) {
// 捕获并显示异常
e.printStackTrace()
} finally {
// 关闭我们使用过的资源
if (rs != null)
try {
rs.close()
} catch (Exception e) {}
if (stmt != null)
try {
stmt.close()
} catch (Exception e) {}
if (con != null)
try {
con.close()
} catch (Exception e) {}
}
%>
</table>
<a href="insert.jsp">添加新纪录</a>
</center>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)