JSP and SQL Server's message book

Source: Internet
Author: User
Tags date count datetime empty insert odbc reset tostring
Js|server


<!--add_message.jsp-->
<%@ page import= "java.sql.*"%>
<HTML>
<HEAD>
<title>add Message into table </TITLE>
</HEAD>
<BODY>
<%
String name=request.getparameter ("name");
String Mail=request.getparameter ("Mail");
String Title=request.getparameter ("title");
String content=request.getparameter ("content");
if (name==null)
Name= "";
if (title==null)
Title= "";
if (content==null)
Content= "";
if (mail==null)
Mail= "";
if (Name.length () ==0)
OUT.PRINTLN ("Message person name cannot be empty!") ");
else if (title.length () ==0)
OUT.PRINTLN ("Message theme can not be empty!") ");
else if (content.length () ==0)
OUT.PRINTLN ("Message content can not be empty!") ");
Else
{
Java.util.Date date=new java.util.Date ();
String datetime=new Timestamp (Date.gettime ()). ToString ();

Try
{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url= "jdbc:odbc:user_db";
Connection con=drivermanager.getconnection (URL, "Insher", "Insher");
PreparedStatement stm=con.preparestatement (INSERT into message values (?,?,?,?,?));
Stm.setstring (1,title);
Stm.setstring (2,name);
if (mail.length () ==0)
Stm.setstring (3,null);
Else stm.setstring (3,mail);
Stm.setstring (4,datetime);
Stm.setstring (5,content);
Try
{
Stm.executequery ();
} catch (Exception e)
{}
Con.close ();
}
catch (Exception e)
{}
%>
<jsp:forward page= "view_message.jsp"/>
<%
} %>
</body>

<!--add_message.jsp--
<%@ page import= "java.sql.*"%>
<HTML>
<HEAD>
<title >add message into table </TITLE>
</HEAD>
<BODY>
<%
String Name=request.getpara Meter ("name");
String Mail=request.getparameter ("Mail");
String title=request.getparameter ("title");
String content=request.getparameter ("content");
if (name==null)
Name= "";
if (title==null)
title= "";
if (content==null)
content= "";
if (mail==null)
mail= "";
if (name.length () ==0)
Out.println ("message person name cannot be empty!") ");
Else if (title.length () ==0)
Out.println ("The message topic cannot be empty!") ");
Else if (content.length () ==0)
Out.println ("message content cannot be empty!") ");
Else
{
Java.util.Date date=new java.util.Date ();
String datetime=new Timestamp (Date.gettime ()). ToString ();

Try
{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url= "jdbc:odbc:user_db";
Connection con=drivermanager.getconnection (URL, "Insher", "Insher");
PreparedStatement stm=con.preparestatement ("INSERT into message values (?,?,?,?,?)");
Stm.setstring (1,title);
Stm.setstring (2,name);
if (Mail.length () ==0)
Stm.setstring (3,null);
else stm.setstring (3,mail);
Stm.setstring (4,datetime);
Stm.setstring (5,content);
Try
{
Stm.executequery ();
catch (Exception e)
{}
Con.close ();
}
catch (Exception e)
{}
%>
<jsp:forward page= "view_message.jsp"/>
<%
}
%>
</body>

<!--index.html-->
<HTML>
<HEAD>
<TITLE> Message Board </TITLE>
</HEAD>
<BODY>
<table width= "90%" align= "center" border=1 cellspacing= "0" cellpadding= "0" bordercolordark= "#88a6dd" bordercolorlight= "#88a6dd" >
<TR>
&LT;TD align= "center" bgcolor= "#88a6dd" > Guest Message Board </TD>
</TR>
<TR>
<td><table align= "center" border=1 cellspacing= "0" cellpadding= "0" bordercolordark= "#88a6dd" bordercolorlight= "#88a6dd" >
<form action= "add_message.jsp" >
<TR>
<TD> name:</td>
&LT;TD bgcolor= "#88a6dd" ><input type= "text" name= "name" size=25></td>
</TR>
<TR>
&LT;TD bgcolor= "#88a6dd" >E-mail:</TD>
&LT;TD bgcolor= "#88a6dd" ><input type= "text" name= "Mail" size=25></td>
</TR>
<TR>
<TD> Theme:</td>
&LT;TD bgcolor= "#88a6dd" ><input type= "text" name= "title" Size=25></td>
</TR>
<TR>
&LT;TD valign= "Top" bgcolor= "#88a6dd" > Message:</td>
<td><textarea name= "Content" rows=7 cols=25></textarea></td>
</TR>
<TR>
&LT;TD colspan=3><table align= "center" width= "100%" cellspacing= "0" cellpadding= "0" bordercolordark= "#88a6dd" bordercolorlight= "#88a6dd" >
<TR>
&LT;TD align= "center" bgcolor= "#88a6dd" ><input type= "submit" value= "Leave a Message" ></TD>
&LT;TD align= "center" ><a href= "view_message.jsp" ><font size=2> View message </font></a></td >
&LT;TD align= "center" bgcolor= "#88a6dd" ><input type= "reset" value= "re-fill" ></TD>
</TR>
</TABLE></TD>
</TR>
</FORM>
</TABLE></TD>
</TR>
</TABLE>
</BODY>
</HTML>

<HTML>
<HEAD>
<TITLE> Message Board </TITLE>
</HEAD>
<BODY>
<table width= "60%" align= "center" border=1 cellspacing= "0" cellpadding= "0" bordercolordark= "#88a6dd" bordercolorlight= "#88a6dd" >
<TR>
&LT;TD align= "center" bgcolor= "#88a6dd" > Guest Message Board </TD>
</TR>
<TR>
<td><table align= "center" border=1 cellspacing= "0" cellpadding= "0" bordercolordark= "#88a6dd" bordercolorlight= "#88a6dd" >
<form action= "add_message.jsp" >
<TR>
<TD> name:</td>
&LT;TD bgcolor= "#88a6dd" ><input type= "text" name= "name" size=25></td>
</TR>
<TR>
&LT;TD bgcolor= "#88a6dd" >E-mail:</TD>
&LT;TD bgcolor= "#88a6dd" ><input type= "text" name= "Mail" size=25></td>
</TR>
<TR>
<TD> Theme:</td>
&LT;TD bgcolor= "#88a6dd" ><input type= "text" name= "title" Size=25></td>
</TR>
<TR>
&LT;TD valign= "Top" bgcolor= "#88a6dd" > Message:</td>
<td><textarea name= "Content" rows=7 cols=25></textarea></td>
</TR>
<TR>
&LT;TD colspan=3><table align= "center" width= "100%" cellspacing= "0" cellpadding= "0" bordercolordark= "#88a6dd" bordercolorlight= "#88a6dd" >
<TR>
&LT;TD align= "center" bgcolor= "#88a6dd" ><input type= "submit" value= "Leave a Message" ></TD>
&LT;TD align= "center" ><a href= "view_message.jsp" ><font size=2> View message </font></a></td >
&LT;TD align= "center" bgcolor= "#88a6dd" ><input type= "reset" value= "re-fill" ></TD>
</TR>
</TABLE></TD>
</TR>
</FORM>
</TABLE></TD>
</TR>
</TABLE>
</BODY>
</HTML>

<!--view_message.jsp-->
<%@ page import= "java.sql.*"%>
<HTML>
<HEAD>
<TITLE> show the message in the table </TITLE>
</HEAD>
<BODY>
<p align= "center" > All guest Messages </p>
<%
Try
{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url= "jdbc:odbc:user_db";
Connection con=drivermanager.getconnection (URL, "Insher", "Insher");
Statement stm=con.createstatement ();
ResultSet result=stm.executequery ("SELECT count (*) from message");
int message_count=0;
if (Result.next ())
{
Message_count=result.getint (1);
Result.close ();
}
if (message_count>0)
{
Result=stm.executequery ("SELECT * from Message ORDER by time Desc");
while (Result.next ())
{
String title=result.getstring ("title");
String name=result.getstring ("name");
String mail=result.getstring ("Mail");
Timestamp Time=result.gettimestamp ("Time");
Date Date_time=new Date (Time.gettime ());
Time Time_time=new Time (Time.gettime ());
String content=result.getstring ("content");
%>
<table width= "100%" align= "center" border=1 cellspacing= "0" cellpadding= "0" bordercolordark= "#000000" bordercolorlight= "#88a6dd" >
&LT;TR&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2> theme:</font></td>
&LT;TD colspan=3><%=title%></td></tr>
&LT;TR&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2> message person:</font></td>
&LT;TD&GT;&LT;%=NAME%&GT;&LT;/TD&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2>e-mail:</font></td >
<td>
<%
Out.println ("<a href=mailto:" +mail+ ">" +mail+ "</a>");
%>
</td></tr>
&LT;TR&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2> message time: &LT;/FONT&GT;&LT;/TD&GT;&LT;TD colspan=3>
<%
Out.println ("<font size=2>" +date_time+ "" +time_time+ "</font>");
%>
</td></tr>
&LT;TR&GT;&LT;TD align= "center" >
<%
Out.println ("(" +message_count+ ")");
%>
</td>
&LT;TD colspan=3><%=content%>
</td></tr>
</table>
<%
Out.println ("message_count--;
}
Result.close ();
Con.close ();
}
Else
{
OUT.PRINTLN ("There are no messages yet!") ");
Con.close ();
}
catch (Exception e)
{out.println (e);}
%>
<p align= "center" ><a href= ". /message/index.html > I want to leave a message </a></p>
</body>

<!--view_message.jsp-->
<%@ page import= "java.sql.*"%>
<HTML>
<HEAD>
<TITLE> show the message in the table </TITLE>
</HEAD>
<BODY>
<p align= "center" > All guest Messages </p>
<%
Try
{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url= "jdbc:odbc:user_db";
Connection con=drivermanager.getconnection (URL, "Insher", "Insher");
Statement stm=con.createstatement ();
ResultSet result=stm.executequery ("SELECT count (*) from message");
int message_count=0;
if (Result.next ())
{
Message_count=result.getint (1);
Result.close ();
}
if (message_count>0)
{
Result=stm.executequery ("SELECT * from Message ORDER by time Desc");
while (Result.next ())
{
String title=result.getstring ("title");
String name=result.getstring ("name");
String mail=result.getstring ("Mail");
Timestamp Time=result.gettimestamp ("Time");
Date Date_time=new Date (Time.gettime ());
Time Time_time=new Time (Time.gettime ());
String content=result.getstring ("content");
%>
<table width= "100%" align= "center" border=1 cellspacing= "0" cellpadding= "0" bordercolordark= "#000000" bordercolorlight= "#88a6dd" >
&LT;TR&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2> theme:</font></td>
&LT;TD colspan=3><%=title%></td></tr>
&LT;TR&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2> message person:</font></td>
&LT;TD&GT;&LT;%=NAME%&GT;&LT;/TD&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2>e-mail:</font></td >
<td>
<%
Out.println ("<a href=mailto:" +mail+ ">" +mail+ "</a>");
%>
</td></tr>
&LT;TR&GT;&LT;TD bgcolor= "#88a6dd" ><font size=2> message time: &LT;/FONT&GT;&LT;/TD&GT;&LT;TD colspan=3>
<%
Out.println ("<font size=2>" +date_time+time_time+ "</font>");
%>
</td></tr>
&LT;TR&GT;&LT;TD align= "center" >
<%
Out.println ("(" +message_count+ ")");
%>
</td>
&LT;TD colspan=3><%=content%>
</td></tr>
</table>
<%
Out.println ("message_count--;
}
Result.close ();
Con.close ();
}
Else
{
OUT.PRINTLN ("There are no messages yet!") ");
Con.close ();
}
catch (Exception e)
{out.println (e);}
%>
<p align= "center" ><a href= ". /message/index.html > I want to leave a message </a></p>
</body>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.