Next we will start to create a message page!
<% @ 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) {}// convert to the format that can be added to 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> use a MYsql database </title>
</Head>
<Body>
<P align = "center"> <center> <font style = "COLOR: blue; FONT-FAMILY: 正, 文,, fon; FONT-SIZE: 16pt "> Ping Hui jsp message board </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 (); // obtain the ip address
String time = (new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss", Locale. US). format (new java. util. Date ());
String userid = "";
String MyQuery = "";
Here, I call a class that checks the message validity.
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) // there is no error in the message, write to the database
Try {// The database is successfully written.
Java. SQL. Connection sqlConn; // database Connection object
Java. SQL. Statement sqlStmt; // Statement object
Java. SQL. ResultSet sqlRst; // result set object
// Register the JDBC driver object
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
// Connect to the database
SqlConn = java. SQL. DriverManager. getConnection ("jdbc: mysql: // localhost/pinghui", "test ","");
// Create a statement object
SqlStmt = sqlConn. createStatement (java. SQL. ResultSet. TYPE_SCROLL_INSENSITIVE, java. SQL. ResultSet. CONCUR_READ_ONLY );
// Execute the SQL statement
Here, a class is called to process messages, which can be recognized by 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.exe cuteQuery (MyQuery); // Add data to the database
SqlRst. close (); // close the result set object
SqlStmt. close (); // close the statement object
SqlConn. close (); // close the database connection
Out. print (time );
%>
<Font size = "5" face = "文 "> the message is sent successfully. Thank you! </Font>
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tbody>
<Tr>
<Td width = "25%"> nickname: <% = username %> </td>
<Td width = "25%"> Gender: <% = sex %> </td>
<Td colspan = "2" width = "50%"> address: <% = address %> </td>
</Tr>
<Tr>
<Td width = "25%"> Tel: <% = telnumber %> </td>
<Td width = "25%"> zip code: <% = 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%"> URL: <a href target = "_ blank"> </a> </td>
</Tr>
<Tr>
<Td colspan = "4"> <font style = "COLOR: green; LINE-HEIGHT: 150%"> message: </font> <% = comment %> <font style = "COLOR: green; LINE-width: 150%"> <br>
-</Font> <% = time %> <font style = "COLOR: green; LINE-HEIGHT: 150% "> (from </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) // failed to write to the database
{Out. print ("<font color = red> Message failed </font> ");
Out. print (MyQuery );
Out. print (userid );
}
Else
{
Out. print ("<font color = red> total" + testcomment. ErrorCount + "error data, Please modify it! </Font> ");
%>
<Script language = "JavaScript">
<! --
Function test_comments (theForm)
{
If (theForm. name. value = "")
{Alert ("name cannot be blank! :-)");
TheForm. name. focus ();
Return (false );
}
If (theForm. name. value. length> 12)
{Alert ("the name is too long and cannot contain more than 12 characters (six Chinese characters )! ");
TheForm. name. focus ();
Return (false );
}
If (theForm. address. value. length> 40)
{Alert ("the address is too long and cannot exceed 40 characters (20 Chinese characters )! ");
TheForm. name. focus ();
Return (false );
}
If (theForm. comment. value = "")
{Alert ("sorry, the message cannot be blank! You must enter a message ");
TheForm. comment. focus ();
Return (false );
}
If (theForm. comment. value. length> 1000)
{Alert ("sorry, your message is too long. It can contain a maximum of 1000 characters! ");
TheForm. comment. focus ();
Return (false );
}
Return (true );
} -->
<Table>
<Tr>
<Td>
<Table cellspacing = "0" cellpadding = "0">
<Tr>
<Td> <! -- Form start -->
<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%"> nickname: <input name = "name" size = "20" value = "<% if (username! = Null) out. print (username); %> "> <% = testcomment. Comment_Er (" username ") %> </td>
<Td nowrap width = "40%"> sex: <select name = "sex" size = "1">
<Option selected value = "male" <% if (sex = "male") out. print ("select"); %> male </option>
<Option value = "" <% if (sex = "") out. print ("select"); %> female </option>
<Option value = "confidential" <% if (sex = "confidential") out. print ("select"); %> confidential </option>
</Select> <% = testcomment. Comment_Er ("sex") %> </td>
</Tr>
<Tr>
<Td colspan = "2"> address: <input name = "address" size = "40" value = "<% if (address! = Null) out. print (address); %> "> <% = testcomment. Comment_Er (" address ") %> </td>
</Tr>
<Tr>
<Td> Tel: <input name = "telphone" size = "15" value = "<% if (telnumber! = Null) out. print (telnumber); %> "> <% = testcomment. Comment_Er (" tel ") %> </td>
<Td> post: <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"> address: <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 = "OK"?> </Td>
<Td align = "left"> <input name = "r" type = "reset" value = "reset"> </td>
</Tr>
</Table>
</Form>
<! -- End of the submission form -->
</Td>
<Td> <% = testcomment. Comment_Er ("comment") %> note: In the entered content, the nickname and message are required items! <Br>
Html flag not supported </td>
</Tr>
</Table>
</Td>
</Tr>
</Table>
<%
}
%>
</Body>