js|mysql|创建
说明:用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 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%">昵 称:<input name="name" size="20" value="<%if (username != null) out.print (username);%>"><%=testcomment.Comment_Er("username")%></td>
<td nowrap width="40%">性 别:<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">地 址:<input name="address" size="40" value="<%if (address!=null) out.print(address);%>"><%=testcomment.Comment_Er("address")%></td>
</tr>
<tr>
<td>电 话:<input name="telphone" size="15" value="<%if(telnumber!=null)out.print(telnumber);%>"><%=testcomment.Comment_Er("tel")%></td>
<td>邮 编:<input name="postal" size="6" value="<%if(post!=null) out.print(post);%>"><%=testcomment.Comment_Er("postal")%></td>
</tr>
<tr>
<td> OICQ:<input name="oicq" size="10" value="<%if(oicq!=null) out.print (oicq);%>"><%=testcomment.Comment_Er("oicq")%></td>
<td> ICQ :<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">网 址:<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")%>请注意:你所输入的内容中,昵称和留言为必须输入的项目!
不支持html标志</td>
</tr>
</table>
</td>
</tr>
</table>
<%
}
%>
</body>