JSP+MySQL을 사용하여 방명록을 만드는 방법 (3)
저자:Eve Cole
업데이트 시간:2009-07-02 17:13:14
아래쪽 면은 우리의 页면을 보여줍니다!
<%@페이지
가져오기 ="java.util.*"
가져오기 ="java.text.*"
import="java.sql.*"
가져오기 ="java.io.*"
가져오기 ="java.lang.*"
contentType="text/html; charset=gb2312"
%>
<%
CommentError 클래스// java.lang.NullPointerException이 발생합니다.
{ 공개 문자열 사용자 이름="",Sex="",Address="",Postal="",Oicq="",Icq="",Tel="",Comment="";
공개 부울 NoError=true;//false;
공개 int ErrorCount=0;
개인 부울 IsNumber(String s1) {}
공개 문자열 글꼴(문자열 se) {}
공개 문자열 Comment_Er(문자열 se) {}
공개 무효 사용자 이름(문자열 se) {}
공개 무효 섹스(문자열 se) {}
공개 무효 주소(문자열 se) {}
공개 무효 주석(문자열 se) {}
공개 무효 전화(문자열 se) {}
공개 무효 우편(문자열 se) {}
공개 무효 Oicq(문자열 se) {}
공개 무효 Icq(문자열 se) {}
}
클래스 형식댓글
{ 공개 문자열 바꾸기(문자열 소스, 문자열 oldString, 문자열 newString) {}
공개 문자열 형식(문자열 se) {}
공개 문자열 fromatcomment(문자열 se) {}
공개 문자열 toHtmlInput(String str) {}
공개 문자열 toHtml(String 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>
</head>
<본문>
<p align="center"><center><font style="COLOR: blue; FONT-FAMILY: 方正舒体,华文行楷,隶书,宋体; FONT-SIZE: 16pt">萍慧jsp留言薄</font ></center></p>
<p 정렬="중앙">
<%! 문자열 사용자 이름, 성별, 주소, 게시물, oicq,icq, 전화번호, 댓글, 이메일, url.urltitle;
%>
<%
try{ 사용자 이름=request.getParameter("이름");
}catch(NullPointerException e){ 사용자 이름="";}
시도{ 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){ 주소="";}
try{ post=request.getParameter("postal");
}catch(NullPointerException e){ post="";}
시도{ oicq=request.getParameter("oicq");
}catch(NullPointerException e){ oicq="";}
시도{ icq=request.getParameter("icq");
}catch(NullPointerException e){ icq="";}
시도{ telnumber=request.getParameter("telphone");
}catch (NullPointerException e) { telnumber= ""; }
시도{ email=request.getParameter("email");
}catch (NullPointerException e) { email= ""; }
시도{ url=request.getParameter("url");
}catch (NullPointerException e) { url= ""; }
시도{ urltitle=request.getParameter("urltitle");
}catch (NullPointerException e) { urltitle= ""; }
문자열 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.Username(사용자 이름);
testcomment.Postal (포스트);
testcomment.Sex(sex);
testcomment.Address(주소);
testcomment.Tel(전화번호);
testcomment.Comment(댓글);
testcomment.Oicq(oicq);
testcomment.Icq(icq);
if (testcomment.NoError) //留言中没有错误,写数据库
{//写数据库成功를 시도해 보세요
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= 새로운 FormatComment();
oicq=FC.formatint(oicq);
icq=FC.formatint(icq);
post=FC.formatint(포스트);
전화번호=FC.formatint(전화번호);
사용자 이름=FC.toSql(사용자 이름);
comment=FC.toSql(코멘트);
MyQuery="댓글에 삽입(username,sex,address,ip,post,oicq,icq,telnumber,comment,time,url,email) 값('"+username+"','"+sex+"','"+ 주소+"','"+ip+"',"+post+","+oicq+","+icq+",'"+telnumber+"','"+comment+"',now(),'"+url+"' ,'"+이메일+"');";
sqlRst=sqlStmt.executeQuery (MyQuery); //向数据库中加入数据
sqlRst.close();//关闭结果集对象
sqlStmt.close();//关闭语句对象
sqlConn.close(); //关闭数据库连接
out.print(시간);
%>
<font size="5" face="华文行楷">留言成功,谢谢!</font>
<table border="0" cellpadding="0" cellpacing="0" width="100%">
<본체>
<tr>
<td width="25%">昵称:<%=사용자 이름%></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: 녹색; LINE-HEIGHT: 150%">표시:</font><%=comment%><font style="COLOR: 녹색; LINE-HEIGHT: 150%"><br>
-</font><%=time %><font style="COLOR: green; LINE-HEIGHT: 150%">(来自</font><%=ip %><font style="COLOR: 녹색; 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(사용자 ID);
}
또 다른
{
out.print("<font color=red>总共有"+testcomment.ErrorCount+"条错误数据,请修改!</font>");
%>
<스크립트 언어="자바스크립트">
<!--
함수 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>
<테이블 셀 간격="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%" cellpacing="0" cellpadding="0">
<tr>
<td width="60%">昵 称:<input name="name" size="20" value="<%if (사용자 이름 != null) out.print (사용자 이름);%>">< %=testcomment.Comment_Er("사용자 이름")%></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("주소")%></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"row="10" Wrap="physical"><%if (comment!=null)out.print (코멘트);%>< /텍스트 영역>
<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>