jsp+mysql制作简单的留言板(4)

80酷酷网    80kuku.com

  js|mysql对于留言板来说,是要存取数据库的;所以opendata.jsp是必不可少的。
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url
="jdbc:mysql://localhost/mydate?user=root&password=zsliuyu&useUnicode=true&characterEncoding=GB2312";

Connection con=DriverManager.getConnection(url);
Statement smt=con.createStatement();
ResultSet rs;
String sql;
%>
opendata.jsp;
留言板必须把留言保存在数据库中,连接好数据库后,我们就要把使用者的的流言存入数据库中:
<% page import="java.sql.*"%>
<% page import="java.util.*"%>
<% include file="opendata.jsp"%>
<% include file="convert.jsp"%>
<% page contentType="text/html;charset=GB2312"%>
<%
String name,email,subject,memo,sex;
name=request.getParameter("name");
sex=request.getParameter("sex");
email=request.getParameter("email");
subject=request.getParameter("subject");
memo=request.getParameter("memo");
if(name.length()==0||email.length()==0||subject.length()==0||memo.length()==0)
{
out.print("<center><font color=red size=6>输入的字段不可为空</font>");
out.print("<hr><a href=board.jsp>回留言板</a>");
out.print("<a href=message.htm>我要留言</a>");

}
else
{
int count=0,year,month,day,hour,minute,second,lastp;
String time;
GregorianCalendar calendar;<%--利用GregorianCalendar类取得时间--%>
calendar=new GregorianCalendar();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH)+1;
day=calendar.get(Calendar.DAY_OF_MONTH);
hour=calendar.get(Calendar.HOUR_OF_DAY);
minute=calendar.get(Calendar.MINUTE);
second=calendar.get(Calendar.SECOND);
time=year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;
name=convert(name);
time=convert(time);
subject=convert(subject);
memo=Replace(memo);
memo=convert(memo);
sql="insert into message set name='"+name+"',email='"+email+"',subject='"+subject+"',time='"+time+"',sex='"+sex+"',memo='"+memo+"'";
smt.executeUpdate(sql);
sql="select * from message";
rs=smt.executeQuery(sql);
while(rs.next())
count++;
lastp=(int)Math.ceil((double)count/5);
response.sendRedirect("board.jsp?pageno="+lastp);
}

%>
<html>
<title>错误信息</title>
</html>

savememo.jsp
这个程序是用来将message.htm的数据存入数据库中




分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: