jsp+mysql製作簡單的留言板(4)
來源:互聯網
上載者:User
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的資料存入資料庫中