欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何使用JSP+MySQL创建留言本(三)

程序员文章站 2023-12-17 16:10:22
下面我们开始建立留言的页面! <%@page import ="java.util.*" import ="java.text.*&...
下面我们开始建立留言的页面!
<%@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的格式
}

%>
<html>
<head>
<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>

<body>

<p align="center"><center><font style="color: blue; font-family: 方正舒体,华文行楷,隶书,宋体; font-size: 16pt">萍慧jsp留言薄</font></center></p>
<p align="center">

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

%>
<font size="5" face="华文行楷">留言成功,谢谢!</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
  <tr>
   <td width="25%">昵称:<%=username%></td>
   <td width="25%">性别:<%=sex%></td>
   <td colspan="2" width="50%">地址:<%=address %></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%">email:<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>
</table>
<hr color="#0080c0" noshade size="1" width="100%">

<%
} catch (sqlexception e) //写数据库失败
{ out.print ("<font color=red>留言失败</font>");
  out.print (myquery);
  out.print (userid);
}
else
{
  out.print ("<font color=red>总共有"+testcomment.errorcount+"条错误数据,请修改!</font>");
%>

<script 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")%>请注意:你所输入的内容中,昵称和留言为必须输入的项目!<br>
      不支持html标志</td>
    </tr>
   </table>
  </td>
</tr>
</table>
<%
}
%>
</body>

上一篇:

下一篇: