如何使用JSP+MySQL创建留言本(三)
作者:Eve Cole
更新时间:2009-07-02 17:13:14
下面我们开始建立留言页面!
<%@页
导入=“java.util.*”
导入=“java.text.*”
导入=“java.sql.*”
导入=“java.io.*”
导入=“java.lang.*”
contentType="text/html; 字符集=gb2312"
%>
<%
类 CommentError // 抛出 java.lang.NullPointerException
{ public String 用户名=“”,性别=“”,地址=“”,邮政=“”,Oicq=“”,Icq=“”,电话=“”,评论=“”;
公共布尔NoError = true; // false;
公共int错误计数=0;
私有布尔 IsNumber(String s1) {}
公共字符串字体(字符串se){}
公共字符串Comment_Er(字符串se){}
公共无效用户名(字符串se){}
公共无效性别(字符串se){}
公共无效地址(字符串se){}
公共无效注释(字符串se){}
公共无效电话(字符串se){}
公共无效邮政(字符串se){}
公共无效Oicq(字符串se){}
公共无效Icq(字符串se){}
}
类格式注释
{ 公共字符串替换(字符串源,字符串oldString,字符串newString){}
公共字符串formatint(字符串se){}
公共字符串 fromatcomment(String se) {}
公共字符串toHtmlInput(字符串str){}
公共字符串toHtml(字符串str){}
public String toSql(String str) {}//转换为可以加入Myqal的格式
}
%>
<html>
<头>
<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>
</头>
<正文>
<palign="center"><center><font style="COLOR: blue; FONT-FAMILY: 方正舒体,华文行楷,隶书,宋体; FONT-SIZE: 16pt">萍慧jsp留言薄</font ></中心></p>
<p对齐=“中心”>
<%! String 用户名、性别、地址、帖子、oicq、icq、电话号码、评论、电子邮件、url.urltitle;
%>
<%
尝试{ 用户名=request.getParameter("name");
}catch (NullPointerException e){ 用户名="";}
尝试{ comment=request.getParameter("comment");
}catch (NullPointerException e){ comment="";}
尝试{ sex=request.getParameter("sex");
}catch (NullPointerException e){ sex="";}
尝试{ 地址=request.getParameter("地址");
}catch (NullPointerException e){ 地址="";}
尝试{ post=request.getParameter("邮政");
}catch (NullPointerException e){ post="";}
尝试{ oicq=request.getParameter("oicq");
}catch (NullPointerException e){ oicq="";}
尝试{ icq=request.getParameter("icq");
}catch (NullPointerException e){ icq="";}
尝试{ telnumber=request.getParameter("电话");
}catch (NullPointerException e) { 电话号码=“”; }
尝试{ email=request.getParameter("email");
}catch (NullPointerException e) { email= ""; }
尝试{ url=request.getParameter("url");
}catch (NullPointerException e) { url= ""; }
尝试{ 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());
字符串用户ID=“”;
字符串 MyQuery="";
这里我引用了一个对留言进行合法检验的类
CommentError testcomment = new CommentError();
测试评论.用户名(用户名);
testcomment.Postal(邮寄);
测试评论.Sex(性别);
测试评论.Address(地址);
测试评论.Tel(电话号码);
测试评论.评论(评论);
测试评论.Oicq(oicq);
测试评论.Icq(icq);
if (testcomment.NoError) //留言中没有错误,写数据库
try {//写数据库成功
java.sql.Connection sqlConn; // 数据库连接对象
java.sql. 语句 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);
电话号码=FC.formatint(电话号码);
用户名=FC.toSql(用户名);
评论=FC.toSql(评论);
MyQuery =“插入评论(用户名,性别,地址,ip,帖子,oicq,icq,电话号码,评论,时间,url,电子邮件)值('“+用户名+”','“+性别+”','“+地址+"','"+ip+"',"+post+","+oicq+","+icq+",'"+电话号码+"','"+评论+"',now(),'"+url+"' ,'"+电子邮件+"');";
sqlRst=sqlStmt.executeQuery(MyQuery); //向数据库中添加数据
sqlRst.close();//关闭结果集对象
sqlStmt.close();//关闭语句对象
sqlConn.close(); //关闭数据库连接
输出.打印(时间);
%>
<font size="5"face="华文行楷">留言成功,谢谢!</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<正文>
<tr>
<td width="25%">昵称:<%=username%></td>
<td width="25%">性别:<%=sex%></td>
<td colspan="2" width="50%">地址:<%=地址 %></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%">邮箱:<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>
-</font><%=time %><font style="COLOR: green; LINE-HEIGHT: 150%">(来自</font><%=ip %><font style="COLOR: green; LINE-HEIGHT: 150%">)</font></td>
</tr>
</tbody>
</表>
<hr color="#0080c0" noshade size="1" width="100%">
<%
} catch (SQLException e) // 写数据库失败
{ out.print("<font color=red>留言失败</font>");
输出.print(MyQuery);
输出.print(用户ID);
}
别的
{
out.print("<font color=red>总共有"+testcomment.ErrorCount+"条错误数据,请修改!</font>");
%>
<脚本语言=“JavaScript”>
<!--
函数 test_comments(theForm)
{
if (theForm.name.value == "")
{alert("姓名不能为空!:-)");
theForm.name.focus();
返回(假);
}
if (theForm.name.value.length > 12)
{alert("姓名太长,不能多于12个字符(既六个汉字)!");
theForm.name.focus();
返回(假);
}
if (theForm.address.value.length > 40)
{alert("地址太长,不能多于40个字符(20个汉字)!");
theForm.name.focus();
返回(假);
}
if (theForm.comment.value == "")
{alert("抱歉,留言不能为空!你必须输入留言");
theForm.comment.focus();
返回(假);
}
if (theForm.comment.value.length > 1000)
{alert("抱歉,你的留言太长,最多为1000个字符!");
theForm.comment.focus();
返回(假);
}
返回(真);
} -->
<表>
<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("用户名")%></td>
<td nowrap width="40%">性别:<select name="sex" size="1">
<option selected value="男" <%if(sex=="男") out.print("select"); %>>男</选项>
<option value="女" <%if(sex=="女") out.print("select"); %>>女</选项>
<option value="保密" <%if(sex=="保密") out.print("select"); %>>保密</选项>
</select><%=testcomment.Comment_Er("性别")%></td>
</tr>
<tr>
<td colspan="2">地地址:<input name="address" size="40" value="<%if (address!=null) out.print(address);%>"><% =testcomment.Comment_Er("地址")%></td>
</tr>
<tr>
<td>电话:<input name="telphone" size="15" value="<%if(telnumber!=null)out.print(telnumber);%>"><%=testcomment.Comment_Er( “电话”)%></td>
<td>邮编:<input name="postal" size="6" value="<%if(post!=null) out.print(post);%>"><%=testcomment.Comment_Er( “邮政”)%></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">邮箱:<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);%>< /文本区域>
<tr>
<tdalign="right"><input name="put_submit" type="submit" value="确定" ?></td>
<tdalign="left"><input name="r" type="reset" value="重置"></td>
</tr>
</表>
</形式>
<!--提交表单结束-->
</td>
<td><%=testcomment.Comment_Er("comment")%>请注意:您所输入的内容中,昵称和留言为必须输入的项目!<br>
不支持html标志</td>
</tr>
</表>
</td>
</tr>
</表>
<%
}
%>
</正文>