① jsp不显示数据库内容,怎么回事
有时候是在数据库的刚刚进行更新的数据没有提交。请提交后,在清除ie缓存或者重启ie(重新开启一个session)来刷新数据。应该就可以看到最新的数据了。
② 刚下载了一个深度学习(JSP)留言板,里面的第二步说导入数据库文件GuestBook_sql2000_DB_backup ,如何做到
这个是备份文件。在企业管理器中,选择所有任务,还原数据库就行了。
③ 怎么样用jsp做留言板啊 主要是不会在里面写有关于数据库的东西
留言板要实现的功能是:浏览的人能留言并能分页的查看留言,管理员能对留言进行处理!
这个留言板由9个小程序组成,分别是:board.jsp;message.html;opendata.jsp;manager.jsp;password.jsp;check.jsp;delete.jsp;convert.jsp
现在说第一个:board.jsp
他的任务是整个留言板的主程序,让使用者留言,并提供分页功能!
在开始之前,我们必须在mysql数据库建立一个mydate的数据库,在mudate中建立名为message的表:
mysql>create table message( name char(20),email char(40 ),subject char(60),time char(60),sex char(10),memo text,id int not null auto_increment,primary key(id));
board.jsp代码:
<html>
<head>
<title>流言板</title>
</head>
<body>
<%@ page import="java.sql.*"%>
<%@ page contenttype="text/html;charset=gb2312"%>
<%@ include file="opendata.jsp"%>
<%
int count=0,lastp,numf,numl,prep,nextp,pageno;
if(request.getparameter("pageno")==null) //pageno:代表页码
pageno=0;
else
pageno=integer.parseint(request.getparameter("pageno"));
sql="select * from message";
rs=smt.executequery(sql);
while(rs.next())
count++; <%-- count:多少留言--%>
lastp=(int)math.ceil((double)count/5);
<%--用来计算此表中有几页留言:ceil返回大于等于其数字参数的最小整数。
math.ceil(number)
必选项number 参数是数值表达式。
说明
返回值为大于等于其数字参数的最小整数。 --%>
if(pageno==0||pageno>lastp)
pageno=lastp;
numf=pageno*5-4; <%--显示留言的第一笔数据的编号_id--%>
numl=numf+4;<%--numl:此页的最后的一笔数据编号id--%>
if(pageno==1)
prep=1; <%-- prep:上一页--%>
else
prep=pageno-1;
if(pageno==lastp)
nextp=lastp;
else
nextp=pageno+1;
sql="select * from message where id between "+numf+" and "+numl;
rs=smt.executequery(sql);
%>
<font size=7 color=green>留言板</font>
<hr>
<center>
<form action=board.jsp method=post>
<table boder=0>
<tr>
<td>目前的页次<font color=red><%=pageno%></font>/<font color=blue><%=lastp%></font></td>
<td><a href=board.jsp?pageno=<%=prep%>>[上一页]</a></td>
<%--将参数pageno传递给程序,依据它来计算numl和numf,再将留言数据通过sql取出--%>
<td><a href=board.jsp?pageno=<%=nextp%>>[下一页]</a></td>
<td><a href=board.jsp?pageno=1>[第一页]</a></td>
<td><a href=board.jsp>[最后一页]</a></td>
<td>输入页次<input type=text size=3 name=pageno></td>
<td><input type=submit name=send value=送出></td>
<td><a href=password.jsp><font color=red size="5"><i>站长专用</i></font></a></td>
</tr>
</table>
</form>
<%
string name,email,subject,time,sex,memo;
while(rs.next())
{
name=rs.getstring(1);
email=rs.getstring(2);
subject=rs.getstring(3);
time=rs.getstring(4);
sex=rs.getstring(5);
memo=rs.getstring(6);
out.print("<center>");
out.print("<table border=1>");
out.print("<tr><td bgcolor=yellow>姓名</td><td>"+name+"</td></tr>");
out.print("<tr><td bgcolor=yellow>e-mail</td><td>"+email+"</td></tr>");
out.print("<tr><td bgcolor=yellow>时间</td><td>"+time+"</td></tr>");
out.print("<tr><td bgcolor=yellow>主题</td><td>"+subject+"</td></tr>");
out.print("<tr><td bgcolor=yellow>留言</td><td>"+memo+"<img src="+sex+"></td></tr>");
out.print("</table><p>");
}
%>
<hr>
<center><a href="message.html">我要留言</a>
<a href=board.jsp>查看留言</a>
</body>
</html>
④ 如何在jsp网页中添加留言板
在jsp网页中添加留言板:
1、网页里有可以添加的文本框等基本控件
2、form表单提交给jsp的控制类写入数据库
3、网页里读取出留言的内容。
基本思路就是这样的。
当然更简单点的做法是写好表单,把留言直接发到邮箱里去。jsp有个邮箱组件是jmail 你可以网络看看jmail如何使用。就可以简单的完成留言功能,直接发邮箱不发到数据库了。当然也同样完成了保存留言的功能啦。
⑤ JSP留言板数据库自动回复乱码问题
应该是字符编码的问题,你可以加个过滤器。
下面的代码是tomcat里带的一个例子。
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding right ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
/**
* <p>Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* @author Craig McClanahan
* @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml中的配置如下
<!-- Example filter to set character encoding on each request -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
⑥ 求jsp编写的简易留言板代码!
LOGIN.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
// 判断是否有错误信息,如果有则打印
// 如果没有此段代码,则显示时会直接打印null
if(request.getAttribute("err")!=null)
{
%>
<h2><%=request.getAttribute("err")%></h2>
<%
}
%>
<form action="login_conf.jsp" method="post">
<table width="80%">
<tr>
<td colspan="2">用户登陆</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登陆">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
LOGIN_CONF.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%!
String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
String DBURL = "jdbc:oracle:thin:@localhost:1521:three" ;
String DBUSER = "scott" ;
String DBPASSWORD = "tiger" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
// 声明一个boolean变量,用于保存用户是否合法的状态
boolean flag = false ;
// 接收参数
String id = request.getParameter("id") ;
String password = request.getParameter("password") ;
%>
<%
String sql = "SELECT name FROM person WHERE id=? and password=?" ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,id) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
if(rs.next())
{
// 用户合法
flag = true ;
// 将用户名保存在session之中
session.setAttribute("uname",rs.getString(1)) ;
}
else
{
// 保存错误信息
request.setAttribute("err","错误的用户名及密码!!!") ;
}
rs.close() ;
pstmt.close() ;
conn.close() ;
}
catch(Exception e)
{}
%>
<%
// 跳转
if(flag)
{
// 用户合法
%>
<jsp:forward page="login_success.jsp"/>
<%
}
else
{
// 用户非法
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
</center>
</body>
</html>
LOGIN_SUCCESS.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<h2>登陆成功</h2>
<h2>欢迎<font color="red" size="12">
<%=session.getAttribute("uname")%>
</font>光临MLDN留言程序</h2>
<h3><a href="list_notes.jsp">进入留言管理页面</a></h3>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
INSERT.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<form action="insert_do.jsp" method="post">
<table>
<tr>
<td colspan="2">添加新留言</td>
</tr>
<tr>
<td>标题:</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="content" cols="30" rows="6"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="添加">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
<h3><a href="list_notes.jsp">回到留言列表页</a></h3>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
INSERT_DO.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
// 进行乱码处理
request.setCharacterEncoding("GB2312") ;
%>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%!
String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
String DBURL = "jdbc:oracle:thin:@localhost:1521:three" ;
String DBUSER = "scott" ;
String DBPASSWORD = "tiger" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
%>
<%
// 声明一个boolean变量
boolean flag = false ;
// 接收参数
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
%>
<%
// 现在note表中的主键是sequence生成
String sql = "INSERT INTO note VALUES(note_sequ.nextVal,?,?,?)" ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,title) ;
pstmt.setString(2,author) ;
pstmt.setString(3,content) ;
pstmt.executeUpdate() ;
pstmt.close() ;
conn.close() ;
// 如果插入成功,则肯定能执行到此段代码
flag = true ;
}
catch(Exception e)
{}
%>
<%
response.setHeader("refresh","2;URL=list_notes.jsp") ;
if(flag)
{
%>
留言添加成功,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
else
{
%>
留言添加失败,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
%>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
LIST_NOTES.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
// 编码转换
request.setCharacterEncoding("GB2312") ;
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%!
String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
String DBURL = "jdbc:oracle:thin:@localhost:1521:three" ;
String DBUSER = "scott" ;
String DBPASSWORD = "tiger" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
// 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示
int i = 0 ;
String sql = null;
String keyword = request.getParameter("keyword") ;
// out.println(keyword) ;
if(keyword==null)
{
// 没有任何查询条件
sql = "SELECT id,title,author,content FROM note" ;
}
else
{
// 有查询条件
sql = "SELECT id,title,author,content FROM note WHERE title like ? or author like ? or content like ?" ;
}
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
// 如果存在查询内容,则需要设置查询条件
if(keyword!=null)
{
// 存在查询条件
pstmt.setString(1,"%"+keyword+"%") ;
pstmt.setString(2,"%"+keyword+"%") ;
pstmt.setString(3,"%"+keyword+"%") ;
}
rs = pstmt.executeQuery() ;
%>
<form action="list_notes.jsp" method="POST">
请输入查询内容:<input type="text" name="keyword">
<input type="submit" value="查询">
</form>
<h3><a href="insert.jsp">添加新留言</a></h3>
<table width="80%" border="1">
<tr>
<td>留言ID</td>
<td>标题</td>
<td>作者</td>
<td>内容</td>
<td>删除</td>
</tr>
<%
while(rs.next())
{
i++ ;
// 进行循环打印,打印出所有的内容,以表格形式
// 从数据库中取出内容
int id = rs.getInt(1) ;
String title = rs.getString(2) ;
String author = rs.getString(3) ;
String content = rs.getString(4) ;
if(keyword!=null)
{
// 需要将数据返红
title = title.replaceAll(keyword,"<font color=\"red\">"+keyword+"</font>") ;
author = author.replaceAll(keyword,"<font color=\"red\">"+keyword+"</font>") ;
content = content.replaceAll(keyword,"<font color=\"red\">"+keyword+"</font>") ;
}
%>
<tr>
<td><%=id%></td>
<td><a href="update.jsp?id=<%=id%>"><%=title%></a></td>
<td><%=author%></td>
<td><%=content%></td>
<td><a href="delete_do.jsp?id=<%=id%>">删除</a></td>
</tr>
<%
}
// 判断i的值是否改变,如果改变,则表示有内容,反之,无内容
if(i==0)
{
// 进行提示
%>
<tr>
<td colspan="5">没有任何内容!!!</td>
</tr>
<%
}
%>
</table>
<%
rs.close() ;
pstmt.close() ;
conn.close() ;
}
catch(Exception e)
{}
%>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
UPDATE.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%!
String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
String DBURL = "jdbc:oracle:thin:@localhost:1521:three" ;
String DBUSER = "scott" ;
String DBPASSWORD = "tiger" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
// 接收参数
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
%>
<%
// 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示
int i = 0 ;
String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
// 设置查询条件
pstmt.setInt(1,id) ;
rs = pstmt.executeQuery() ;
%>
<%
if(rs.next())
{
i++ ;
// 进行循环打印,打印出所有的内容,以表格形式
// 从数据库中取出内容
id = rs.getInt(1) ;
String title = rs.getString(2) ;
String author = rs.getString(3) ;
String content = rs.getString(4) ;
%>
<form action="update_do.jsp" method="post">
<table>
<tr>
<td colspan="2">添加新留言</td>
</tr>
<tr>
<td>标题:</td>
<td><input type="text" name="title" value="<%=title%>"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author" value="<%=author%>"></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="content" cols="30" rows="6"><%=content%></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="id" value="<%=id%>">
<input type="submit" value="更新">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
<%
}
else
{
%>
没有发现,要更新的内容!!<br>
请确认要更新的留言是否存在!!<br>
<%
}
%>
<%
rs.close() ;
pstmt.close() ;
conn.close() ;
}
catch(Exception e)
{}
%>
<h3><a href="list_notes.jsp">回到留言列表页</a></h3>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
UPDATE_DO.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
// 进行乱码处理
request.setCharacterEncoding("GB2312") ;
%>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%!
String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
String DBURL = "jdbc:oracle:thin:@localhost:1521:three" ;
String DBUSER = "scott" ;
String DBPASSWORD = "tiger" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
%>
<%
// 声明一个boolean变量
boolean flag = false ;
// 接收参数
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
%>
<%
// 更新note表中的数据
String sql = "UPDATE note set title=?,author=?,content=? WHERE id=?" ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,title) ;
pstmt.setString(2,author) ;
pstmt.setString(3,content) ;
pstmt.setInt(4,id);
pstmt.executeUpdate() ;
pstmt.close() ;
conn.close() ;
// 如果修改成功,则肯定能执行到此段代码
flag = true ;
}
catch(Exception e)
{}
%>
<%
response.setHeader("refresh","2;URL=list_notes.jsp") ;
if(flag)
{
%>
留言修改成功,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
else
{
%>
留言修改失败,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
%>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
DELETE_DO.JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>JSP+JDBC 留言管理程序――登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 ―― JSP + JDBC实现</h1>
<hr>
<br>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%!
String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
String DBURL = "jdbc:oracle:thin:@localhost:1521:three" ;
String DBUSER = "scott" ;
String DBPASSWORD = "tiger" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
%>
<%
// 接收参数
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
%>
<%
String sql = "DELETE FROM note WHERE id=?" ;
boolean flag = false ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;
// 设置删除条件
pstmt.setInt(1,id) ;
pstmt.executeUpdate() ;
pstmt.close() ;
conn.close() ;
flag = true ;
}
catch(Exception e)
{}
%>
<%
response.setHeader("refresh","2;URL=list_notes.jsp") ;
if(flag)
{
%>
留言删除成功,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
else
{
%>
留言删除失败,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
%>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
-- 创建表
-- 用户表(登陆)、留言表
-- 删除表
DROP TABLE person ;
DROP TABLE note ;
-- 删除序列
DROP SEQUENCE note_sequ ;
-- 创建序列
CREATE SEQUENCE note_sequ ;
-- 创建person表
CREATE TABLE person
(
id varchar(20) not null primary key ,
name varchar(20) ,
password varchar(20)
) ;
-- 创建留言表
CREATE TABLE note
(
id int not null primary key , -- sequence
title varchar(20) not null ,
author varchar(20) not null ,
content varchar(50) not null
) ;
-- 插入测试数据
INSERT INTO person VALUES ('LXH','李兴华','zzzzzz') ;
INSERT INTO person VALUES ('MLDN','魔乐','mmmmmm') ;
-- 事务提交
commit ;
⑦ jsp留言板控件
你用SOAOffice中间件吧,这是一个专门服务微软Office的中间件,除了能在线编辑Word/Excel,电子印章、手写批注,还能导入导出Word/Excel数据,完全后台代码调用
⑧ JSP简易留言板,不需要写进数据库,点击“留言”后按后留言者在前显示出来。捉急!
我已经做好了 只不过找不到 刷新的办法!!!有缺陷
留言顺序ABCDEFGHIJK
⑨ jsp页面信息提交数据库没错误提示但是数据库没有数据!怎么回事啊
如果是连接oracle,要这样连:
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test"; String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
其中你要把oracle的驱动文件,一个压缩包放到工程的lib目录下。
如果是连接mysql,就把forName里的换成是com.mysql.jdbc.Driver就行,然后也把相应的驱动加载到lib下
如果你的JdbcUtil已经封装了这些东西也行。