主頁 > 知識庫 > 如何使用JSP+MySQL創(chuàng)建留言本(三)

如何使用JSP+MySQL創(chuàng)建留言本(三)

熱門標簽:地圖標注創(chuàng)業(yè)項目入駐 電銷外呼系統(tǒng) 排行榜 珠海銷售外呼系統(tǒng)運營商 廣州三五防封電銷卡 外呼系統(tǒng)啥意思 山東智能云外呼管理系統(tǒng) 長春回撥外呼系統(tǒng)廠家 地圖標注制作道路 四川電信外呼系統(tǒng)靠譜嗎
下面我們開始建立留言的頁面!
%@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) {}//轉(zhuǎn)換為可以加入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數(shù)據(jù)庫/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="";
此處我調(diào)用了一個對留言進行合法檢驗的類
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) //留言中沒有錯誤,寫數(shù)據(jù)庫                      
try {//寫數(shù)據(jù)庫成功                      
  java.sql.Connection sqlConn; //數(shù)據(jù)庫連接對象
  java.sql.Statement sqlStmt; //語句對象
  java.sql.ResultSet sqlRst; //結(jié)果集對象
  //登記JDBC驅(qū)動對象
  Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
  //連接數(shù)據(jù)庫
  sqlConn= java.sql.DriverManager.getConnection ("jdbc:mysql://localhost/pinghui","test","");
  //創(chuàng)建語句對象
  sqlStmt=sqlConn.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
  //執(zhí)行Sql語句
此處調(diào)用了一個對留言進行處理的類,是留言能被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); //向數(shù)據(jù)庫中加入數(shù)據(jù)
   sqlRst.close();//關(guān)閉結(jié)果集對象
  sqlStmt.close ();//關(guān)閉語句對象
  sqlConn.close(); //關(guān)閉數(shù)據(jù)庫連接
  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%">網(wǎng)址: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>
    nbsp;nbsp;nbsp;nbsp;-/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) //寫數(shù)據(jù)庫失敗
{ out.print ("font color=red>留言失敗/font>");
  out.print (MyQuery);
  out.print (userid);
}
else
{
  out.print ("font color=red>總共有"+testcomment.ErrorCount+"條錯誤數(shù)據(jù),請修改!/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%">昵nbsp;稱:input name="name" size="20" value="%if (username != null) out.print (username);%>">%=testcomment.Comment_Er("username")%>/td>
         td nowrap width="40%">性nbsp;別: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">地nbsp;址:input name="address" size="40" value="%if (address!=null) out.print(address);%>">%=testcomment.Comment_Er("address")%>/td>
        /tr>
        tr>
         td>電nbsp;話:input name="telphone" size="15" value="%if(telnumber!=null)out.print(telnumber);%>">%=testcomment.Comment_Er("tel")%>/td>
         td>郵nbsp;編:input name="postal" size="6" value="%if(post!=null) out.print(post);%>">%=testcomment.Comment_Er("postal")%>/td>
        /tr>
        tr>
         td>nbsp;OICQ:input name="oicq" size="10" value="%if(oicq!=null) out.print (oicq);%>">%=testcomment.Comment_Er("oicq")%>/td>
         td>nbsp;ICQnbsp;: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">網(wǎng)nbsp;址: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>
      !--提交表單結(jié)束-->
     /td>
     td>%=testcomment.Comment_Er("comment")%>請注意:你所輸入的內(nèi)容中,昵稱和留言為必須輸入的項目!br>
      不支持html標志/td>
    /tr>
   /table>
  /td>
/tr>
/table>
%
}
%>
/body>
您可能感興趣的文章:
  • 如何使用JS獲取IE上傳文件路徑(IE7,8)
  • 如何使用json在前后臺進行數(shù)據(jù)傳輸實例介紹
  • 如何使用JSP連接DB2數(shù)據(jù)庫
  • 如何使用JSP+MySQL創(chuàng)建留言本(二)
  • 如何使用JSP+MySQL創(chuàng)建留言本(一)
  • 如何使用JSP訪問MySQL數(shù)據(jù)庫
  • 詳解如何較好的使用js

標簽:玉樹 潮州 北海 廣元 保定 肇慶 紹興 吳忠

巨人網(wǎng)絡(luò)通訊聲明:本文標題《如何使用JSP+MySQL創(chuàng)建留言本(三)》,本文關(guān)鍵詞  如何,使用,JSP+MySQL,創(chuàng)建,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《如何使用JSP+MySQL創(chuàng)建留言本(三)》相關(guān)的同類信息!
  • 本頁收集關(guān)于如何使用JSP+MySQL創(chuàng)建留言本(三)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章

    上一篇:用js繪圖

    下一篇:JDBC 入門(三)