下面我们开始建立留言的页面!
1@page
2import ="java.util.*"
3import ="java.text.*"
4import="java.sql.*"
5import ="java.io.*"
6import ="java.lang.*"
7contentType="text/html; charset=gb2312"
1
2class CommentError// throws java.lang.NullPointerException
3{ public String Username="",Sex="",Address="",Postal="",Oicq="",Icq="",Tel="",Comment="";
4public boolean NoError=true;//false;
5public int ErrorCount=0;
6private boolean IsNumber(String s1) {}
7public String font (String se) {}
8public String Comment_Er(String se) {}
9public void Username (String se) {}
10public void Sex (String se) {}
11public void Address (String se) {}
12public void Comment (String se) {}
13public void Tel (String se) {}
14public void Postal (String se) {}
15public void Oicq (String se) {}
16public void Icq (String se) {}
17}
18class FormatComment
19{ public String Replace(String source, String oldString, String newString) {}
20public String formatint(String se) {}
21public String fromatcomment(String se) {}
22public String toHtmlInput(String str) {}
23public String toHtml(String str) {}
24public String toSql(String str) {}//转换为可以加入Myqal的格式
25}
26
1<html>
2<head>
3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
4<meta content="Microsoft FrontPage 4.0" name="GENERATOR"/>
5<meta content="FrontPage.Editor.Document" name="ProgId"/>
6<title>使用MYsql数据库</title>
7</head>
8<body>
9<p align="center"><center><font style="COLOR: blue; FONT-FAMILY: 方正舒体,华文行楷,隶书,宋体; FONT-SIZE: 16pt">萍慧jsp留言薄</font></center></p>
10<p align="center">
11
! 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);
1<font face="华文行楷" size="5">留言成功,谢谢!</font>
2<table border="0" cellpadding="0" cellspacing="0" width="100%">
3<tbody>
4<tr>
5<td width="25%">昵称:```
6=username
7```</td>
8<td width="25%">性别:```
9=sex
10```</td>
11<td colspan="2" width="50%">地址:```
12=address
13```</td>
14</tr>
15<tr>
16<td width="25%">电话:```
17=telnumber
18```</td>
19<td width="25%">邮编:```
20=post
21```</td>
22<td width="25%">OICQ:```
23=oicq
24```</td>
25<td width="25%">ICQ:```
26=icq
27```</td>
28</tr>
29<tr>
30<td colspan="2" width="50%">Email:<a href="mailto:" title="写信给留言者"></a></td>
31<td colspan="2" width="50%">网址:<a href="" target="_blank"></a></td>
32</tr>
33<tr>
34<td colspan="4"><font style="COLOR: green; LINE-HEIGHT: 150%">留言:</font>```
35=comment
36```<font style="COLOR: green; LINE-HEIGHT: 150%"><br/>
37 -</font>```
38=time
39```<font style="COLOR: green; LINE-HEIGHT: 150%">(来自</font>```
40=ip
41```<font style="COLOR: green; LINE-HEIGHT: 150%">)</font></td>
42</tr>
43</tbody>
44</table>
45<hr color="#0080c0" noshade="" size="1" width="100%"/>
46
} catch (SQLException e) //写数据库失败
{ out.print ("<font color="red">留言失败</font>");
out.print (MyQuery);
out.print (userid);
}
else
{
out.print ("<font color="red">总共有"+testcomment.ErrorCount+"条错误数据,请修改!</font>");
1
2<script language="JavaScript"></script></p></body></html>