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的資料存入資料庫中




相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。