动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Asp → 阅读文章

 如何使用JSP+MySQL创建留言本

作者来源: 
阅读 数 163 人次 , 2006-3-29 4:22:00 


说明:用jsp+mysql数据库创建留言本,首先要创建mysql数据库,关于mysql的使用请到 http://pinghui.51.net/download/012mysql.chm 下载教程。现在为了说明的需要,我们假设数据库已经建立完成,数据库的名称是pinghui,其中有comment表是记录留言信息的。

数据库的结构为:
+-----------+-------------+------+-----+---------------------+----------------+
| field | type | null | key | default | extra |
+-----------+-------------+------+-----+---------------------+----------------+
| userid | int(11) | | pri | 0 | auto_increment |
| username | char(20) | | | | |
| sex | char(2) | | | | |
| address | char(40) | yes | | null | |
| ip | char(15) | | | | |
| post | int(11) | yes | | 0 | |
| oicq | int(11) | yes | | 0 | |
| icq | int(11) | yes | | 0 | |
| telnumber | char(30) | yes | | null | |
| comment | text | | | null | |
| time | datetime | | | 0000-00-00 00:00:00 | |
+-----------+-------------+------+-----+---------------------+----------------+

如果你认为建立数据库太麻烦的话
建立数据库的语句为:
drop database if exists pinghui;
create database pinghui;
use pinghui;
create table comment(
userid int not null default 0 auto_increment primary key,
username char(20) not null,
sex char(2),
address char(40),
ip char(15) not null,
post int default 0,
oicq int default 0,
icq int default 0,
telnumber char(30),
comment text not null,
time datetime not null
);
insert into comment (username,ip,comment,time) values ("pinghui","127.0.0.1",
"你好,请到萍慧自由空间查看信息!",now());
insert into comment (username,sex,address,ip,post,oicq,icq,telnumber,comment,time)
values ('pinghui','男','地址','127.0.0.1',250100,2269101,74875874,'0531-8605449',
'你好,请到萍慧自由空间查看留言!谢谢!',now());
有了数据库下面我们的讲解会更方便的。 继续留言本的建立

有了数据库下面就要对数据库操作读取留言了!
下面列出显示留言的主要程序
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contenttype="text/html; charset=gb2312"
%>
<html>
<head>
<title>萍慧自由空间留言本</title>
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
<style type="text/css"><!--
body { font-size: 9pt}
td { font-size: 9pt}-->
</style>
</head>
<body>
<p align="center"><u><font size="5" face="华文新魏">萍慧自由空间留言本</font></u></p>
<table width="75%" border="1" bgcolor="#ffccff" bordercolorlight="#0000ff"
bordercolordark="#6666ff" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="5" height="202">
<%//下面处理用户的分页请求
string string_page;
int page,recoderpage,recoderrow;
try{string_page=request.getparameter("page");
}catch (nullpointerexception e){string_page="";}
try{page=integer.parseint(string_page);
}catch(numberformatexception e)
{page=0;
}

java.sql.connection sqlconn; //数据库连接对象
java.sql.statement sqlstmt; //语句对象
java.sql.resultset sqlrst; //结果集对象
//登记jdbc驱动对象
class.forname ("org.gjt.mm.mysql.driver").newinstance ();
//连接数据库
sqlconn= java.sql.drivermanager.getconnection ("jdbc:mysql://localhost/p","test","");
//创建语句对象
sqlstmt=sqlconn.createstatement
(java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only);
//执行sql语句
string sqlquery="select count(*) from comment";
sqlrst=sqlstmt.executequery (sqlquery);
sqlrst.next();
int count=sqlrst.getint(1); //取得总的留言记录数
if (page>=0)recoderpage=page;//处理页面
else recoderpage=0-page*10;
if (recoderpage>count/15){ recoderpage=count/15; page=recoderpage; }//页面越界

recoderrow=recoderpage*15; //取得要显示的留言记录号
sqlquery="select * from comment order by userid desc limit "+recoderrow+",15;"; //一次读取15条记录
sqlrst=sqlstmt.executequery (sqlquery);
%> 总共有<%=count%>条留言
<% while (sqlrst.next()) //显示留言
{ //取得下一条记录 %> 第<%=sqlrst.getstring("userid")%>条
<table width="95%" border="1" cellspacing="1" cellpadding="1"
bordercolorlight="#6666ff" bordercolordark="#6666ff" bgcolor="#ccccff" align="center">
<tr>
<td width="25%">昵称:<%=sqlrst.getstring("username")%></td>
<td width="25%">性别:<%=sqlrst.getstring("sex")%></td>
<td colspan="2" width="50%">地址:<%=sqlrst.getstring("address") %></td>
</tr>
<tr>
<td width="25%">电话:<%=sqlrst.getstring("telnumber")%></td>
<td width="25%">邮编:<%=sqlrst.getstring("post")%></td>
<td width="25%">oicq:<%=sqlrst.getstring("oicq")%></td>
<td width="25%">icq:<%=sqlrst.getstring("icq")%></td>
</tr>
<tr>
<td colspan="2" width="50%">email:
<a href="mailto:<%=sqlrst.getstring("email")%>" title="写信给留言者"><%=sqlrst.getstring("email")%></a></td>
<td colspan="2" width="50%">网址:
<a href target="_blank"></a><a href="mailto:<%=sqlrst.getstring("url")%>" title="写信给留言者">
<%=sqlrst.getstring("urltitle")%></a></td>
</tr>
<tr>
<td colspan="4"><font style="line-height: 150%;color: green">
留言:<%=sqlrst.getstring("comment")%><br>
&nbsp;&nbsp;&nbsp;&nbsp;-<%=sqlrst.getstring("time") %>
(来自<%=sqlrst.getstring("ip") %>)</font></td>
</tr>
</table>
<hr align="center" noshade size="2" width="95%">
<% } %> <%
//下面对留言进行分页
int i=count/15; //总页,每页显示15条记录
int j=i/10; //总的大页 ,没10个页面分一大页
//page 显示的页面
int startpage;
//int hrefpage;
if (page<0) page=0-page*10; //10页分一次
startpage=page/10; //10页分一次
out.print ("[共有"+(i+1)+"页]");
//out.print ("共有"+j+"屏");
//out.print ("显示第"+page+"页");
//out.print ("startpage="+startpage);
if (startpage>0)
out.print ("|<a href="connectmysql.jsp?page=-"+integer.tostring(startpage-1)+"">前10页</a>");
for (int k=0;k<10;k++)
{ int p=startpage*10+k;
if (p>i) break;
if (p==page)
out.print ("|第"+integer.tostring(p+1)+"页");
else
out.print ("|<a href="connectmysql.jsp?page="+p+"">第"+integer.tostring(p+1)+"页</a>");
}
if (startpage<j)
out.print ("|<a href="connectmysql.jsp?page=-"+integer.tostring(startpage+1)+"">后10页</a>");
out.print ("|");
%> </td>
</tr>
</table>
</body>
<%
//关闭结果集对象
sqlrst.close();
//关闭语句对象
sqlstmt.close ();
//关闭数据库连接
sqlconn.close();
%> 程序结束
程序中的不足:
没有对错误进行捕捉,但是这里只为了对jsp读取mysql数据库进行讲解。如果是用与作留言本的话一定要对错误进行处理! 我们还要留言呢?

下面我们开始建立留言的页面!
<%@page
import ="java.util.*"
import ="java.text.*"
import="java.sql.*"
import ="java.io.*"
import ="java.lang.*"
contenttype="text/html; charset=gb2312"
%>
<%
class commenterror// throws java.lang.nullpointerexception
{ public string username="",sex="",address="",postal="",oicq="",icq="",tel="",comment="";
public boolean noerror=true;//false;
public int errorcount=0;
private boolean isnumber(string s1) {}
public string font (string se) {}
public string comment_er(string se) {}
public void username (string se) {}
public void sex (string se) {}
public void address (string se) {}
public void comment (string se) {}
public void tel (string se) {}
public void postal (string se) {}
public void oicq (string se) {}
public void icq (string se) {}
}
class formatcomment
{ public string replace(string source, string oldstring, string newstring) {}
public string formatint(string se) {}
public string fromatcomment(string se) {}
public string tohtmlinput(string str) {}
public string tohtml(string str) {}
public string tosql(string str) {}//转换为可以加入myqal的格式
}

%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
<title>使用mysql数据库</title>
</head>

<body>

<p align="center"><center><font style="color: blue; font-family: 方正舒体,华文行楷,隶书,宋体; font-size: 16pt">萍慧jsp留言薄</font></center></p>
<p align="center">

<%! string username,sex,address,post,oicq,icq,telnumber,comment,email,url.urltitle;
%>
<%
try{ username=request.getparameter("name");
}catch (nullpointerexception e){ username="";}
 
try{ comment=request.getparameter("comment");
}catch (nullpointerexception e){ comment="";}
 
try{ sex=request.getparameter("sex");
}catch (nullpointerexception e){ sex="";}
 
try{ address=request.getparameter("address");
}catch (nullpointerexception e){ address="";}
 
try{ post=request.getparameter("postal");
}catch (nullpointerexception e){ post="";}
 
try{ oicq=request.getparameter("oicq");
}catch (nullpointerexception e){ oicq="";}
 
try{ icq=request.getparameter("icq");
}catch (nullpointerexception e){ icq="";}
 
try{ telnumber=request.getparameter("telphone");
}catch (nullpointerexception e) { telnumber= ""; }
try{ email=request.getparameter("email");
}catch (nullpointerexception e) { email= ""; }
try{ url=request.getparameter("url");
}catch (nullpointerexception e) { url= ""; }
try{ urltitle=request.getparameter("urltitle");
}catch (nullpointerexception e) { urltitle= ""; }

string ip=request.getremoteaddr();//得到ip地址
string time=(new simpledateformat ("yyyy-mm-dd hh:mm:ss", locale.us )).format(new java.util.date());
string userid="";
string myquery="";
此处我调用了一个对留言进行合法检验的类
commenterror testcomment= new commenterror();
testcomment.username(username);
testcomment.postal (post);
testcomment.sex(sex);
testcomment.address(address);
testcomment.tel(telnumber);
testcomment.comment(comment);  
testcomment.oicq(oicq);
testcomment.icq(icq);

if (testcomment.noerror) //留言中没有错误,写数据库
try {//写数据库成功
java.sql.connection sqlconn; //数据库连接对象
java.sql.statement sqlstmt; //语句对象
java.sql.resultset sqlrst; //结果集对象
//登记jdbc驱动对象
class.forname ("org.gjt.mm.mysql.driver").newinstance ();
//连接数据库
sqlconn= java.sql.drivermanager.getconnection ("jdbc:mysql://localhost/pinghui","test","");
//创建语句对象
sqlstmt=sqlconn.createstatement (java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only);
//执行sql语句
此处调用了一个对留言进行处理的类,是留言能被mysql承认,
formatcomment fc= new formatcomment();
oicq=fc.formatint(oicq);
icq=fc.formatint(icq);
post=fc.formatint(post);
telnumber=fc.formatint(telnumber);  
username=fc.tosql(username);  
comment=fc.tosql(comment);  
myquery="insert into comment (username,sex,address,ip,post,oicq,icq,telnumber,comment,time,url,email) values ('"+username+"','"+sex+"','"+address+"','"+ip+"',"+post+","+oicq+","+icq+",'"+telnumber+"','"+comment+"',now(),'"+url+"','"+email+"');";
sqlrst=sqlstmt.executequery (myquery); //向数据库中加入数据
 sqlrst.close();//关闭结果集对象
sqlstmt.close ();//关闭语句对象
sqlconn.close(); //关闭数据库连接
out.print (time);
 
%>
<font size="5" face="华文行楷">留言成功,谢谢!</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
 <td width="25%">昵称:<%=username%></td>
 <td width="25%">性别:<%=sex%></td>
 <td colspan="2" width="50%">地址:<%=address %></td>
</tr>
<tr>
 <td width="25%">电话:<%=telnumber%></td>
 <td width="25%">邮编:<%=post%></td>
 <td width="25%">oicq:<%=oicq%></td>
 <td width="25%">icq:<%=icq%></td>
</tr>
<tr>
 <td colspan="2" width="50%">email:<a href="mailto:" title="写信给留言者"></a></td>
 <td colspan="2" width="50%">网址:<a href target="_blank"></a></td>
</tr>
<tr>
 <td colspan="4"><font style="color: green; line-height: 150%">留言:</font><%=comment%><font style="color: green; line-height: 150%"><br>
&nbsp;&nbsp;&nbsp;&nbsp;-</font><%=time %><font style="color: green; line-height: 150%">(来自</font><%=ip %><font style="color: green; line-height: 150%">)</font></td>
</tr>
</tbody>
</table>
<hr color="#0080c0" noshade size="1" width="100%">

<%
} catch (sqlexception e) //写数据库失败
{ out.print ("<font color=red>留言失败</font>");
out.print (myquery);
out.print (userid);
}
else
{
out.print ("<font color=red>总共有"+testcomment.errorcount+"条错误数据,请修改!</font>");
%>

<script language="javascript">
<!--
function test_comments(theform)
{
if (theform.name.value == "")
{ alert("姓名不能为空!:-)");
theform.name.focus();
return (false);
}
if (theform.name.value.length > 12)
{ alert("姓名太长,不能多于12个字符(既六个汉字)!");
theform.name.focus();
return (false);
}

if (theform.address.value.length > 40)
{ alert("地址太长,不能多于40个字符(20个汉字)!");
theform.name.focus();
return (false);
}

if (theform.comment.value == "")
{ alert("抱歉,留言不能为空!你必须输入留言");
theform.comment.focus();
return (false);
}
if (theform.comment.value.length > 1000)
{ alert("抱歉,你的留言太长,最多为1000个字符!");
theform.comment.focus();
return (false);
}
return (true);
} -->
<table>
<tr>
<td>
 <table cellspacing="0" cellpadding="0">
<tr>
 <td><!--表单开始-->
<form action="addmysql.jsp" method="post" name="all_comments" onsubmit="javascript:return (test_comments(this))">
 <table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
 <td width="60%">昵&nbsp;称:<input name="name" size="20" value="<%if (username != null) out.print (username);%>"><%=testcomment.comment_er("username")%></td>
 <td nowrap width="40%">性&nbsp;别:<select name="sex" size="1">
 <option selected value="男" <%if(sex=="男") out.print ("select"); %>>男</option>
 <option value="女" <%if(sex=="女") out.print ("select"); %>>女</option>
 <option value="保密" <%if(sex=="保密") out.print ("select"); %>>保密</option>
</select><%=testcomment.comment_er("sex")%></td>
</tr>
<tr>
 <td colspan="2">地&nbsp;址:<input name="address" size="40" value="<%if (address!=null) out.print(address);%>"><%=testcomment.comment_er("address")%></td>
</tr>
<tr>
 <td>电&nbsp;话:<input name="telphone" size="15" value="<%if(telnumber!=null)out.print(telnumber);%>"><%=testcomment.comment_er("tel")%></td>
 <td>邮&nbsp;编:<input name="postal" size="6" value="<%if(post!=null) out.print(post);%>"><%=testcomment.comment_er("postal")%></td>
</tr>
<tr>
 <td>&nbsp;oicq:<input name="oicq" size="10" value="<%if(oicq!=null) out.print (oicq);%>"><%=testcomment.comment_er("oicq")%></td>
 <td>&nbsp;icq&nbsp;:<input name="icq" size="15" value="<%if(icq!=null)out.print(icq);%>"><%=testcomment.comment_er("icq")%></td>
</tr>
<tr>
 <td colspan="2">email:<input name="email" size="25" value="<%if(email!=null) out.print(email);%>"><%//=testcomment.comment_er("")%></td>
</tr>
<tr>
 <td colspan="2">网&nbsp;址:<input name="url" size="40" value="<%if(url!=null)out.print(url);%>"><%//=testcomment.comment_er("")%></td>
</tr>
<tr>
 <td colspan="2"><textarea cols="80" name="comment" rows="10" wrap="physical"><%if (comment!=null)out.print (comment);%></textarea>
<tr>
 <td align="right"><input name="put_submit" type="submit" value="确定" ?></td>
 <td align="left"><input name="r" type="reset" value="重置"></td>
</tr>
 </table>
</form>
<!--提交表单结束-->
 </td>
 <td><%=testcomment.comment_er("comment")%>请注意:你所输入的内容中,昵称和留言为必须输入的项目!<br>
不支持html标志</td>
</tr>
 </table>
</td>
</tr>
</table>
<%
}
%>
</body>

 本文Tagsmysql  数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:使用JSP读取客户端信息
· 下一篇:JSP数据库操作例程(Use Bean)
· IIS5.0中EXECUTE的巧用
· LINE9的目录浏览源程序
· 深入研究Application和Session对象(包括global.asa)2
· 域名专题 - Win2000功能特性:ActiveDirectory解析
· 把NT 4.0下的ASP应用迁移到Windows 2000上(1.2)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077