当前位置:Gxlcms > mysql > 如何使用JSP+MySQL创建留言本_MySQL

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

时间:2021-07-01 10:21:17 帮助过:24人阅读

说明:用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",
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")%>条
bordercolorlight="#6666FF" bordercolordark="#6666FF" bgcolor="#CCCCFF" align="center">


















昵称:<%=sqlRst.getString("username")%> 性别:<%=sqlRst.getString("sex")%> 地址:<%=sqlRst.getString("address") %>
电话:<%=sqlRst.getString("telnumber")%> 邮编:<%=sqlRst.getString("post")%> OICQ:<%=sqlRst.getString("oicq")%> ICQ:<%=sqlRst.getString("icq")%>
Email:
" title="写信给留言者"><%=sqlRst.getString("email")%>
网址:
" title="写信给留言者">
<%=sqlRst.getString("urltitle")%>

留言:<%=sqlRst.getString("comment")%>

-<%=sqlRst.getString("time") %>
(来自<%=sqlRst.getString("ip") %>)



<% } %> <%
//下面对留言进行分页
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 ("|前10页");
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 ("|第"+Integer.toString(p+1)+"页");
}
if (StartPage out.print ("|后10页");
out.print ("|");
%>



<%
//关闭结果集对象
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的格式
}

%>





使用MYsql数据库




萍慧jsp留言薄




<%! 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);

%>
留言成功,谢谢!






















昵称:<%=username%> 性别:<%=sex%> 地址:<%=address %>
电话:<%=telnumber%> 邮编:<%=post%> OICQ:<%=oicq%> ICQ:<%=icq%>
Email: 网址:
留言:<%=comment%>

-
<%=time %>(来自<%=ip %>




<%
} catch (SQLException e) //写数据库失败
{ out.print ("留言失败");
out.print (MyQuery);
out.print (userid);
}
else
{
out.print ("总共有"+testcomment.ErrorCount+"条错误数据,请修改!");
%>