JSP实训报告Word文档格式.docx
《JSP实训报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《JSP实训报告Word文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
②视图:
表示层。
即与用户实现交互的界面,通常实现数据的输入和输出功能。
③控制器:
控制层。
起到控制整个业务流程的作用,实现视图和模型部分的协同工作。
(3)数据库设计
在SQLServer中新建数据库message,新建数据表mas,其中“留言id”字段数值为自动编码,表的结构如图所示:
3、项目实现(依据MVC模式)
(1)模型(DB.com)
DB.conn代码:
packageapp63;
importjava.sql.*;
publicclassDBconn{
StringDBDriver="
sun.jdbc.odbc.Jdbc.OdbcDriver"
;
StringConnstr="
jdbc:
odbc:
message"
Connectioncon=null;
ResultSetrs=null;
publicDBconn(){
try{
Class.forName(DBDriver);
}
catch(ClassNotFoundExceptione){
System.err.println("
DBconn:
classnotfoundexception"
);
publicvoidexecuteInsert(Strings){
inti=0;
Statementsql;
//con=DriverMananger.get.Connection(Connstr,username,password);
con=DriverManager.getConnection(Connstr);
sql=con.createStatement();
i=sql.executeUpdate(s);
catch(SQLExceptionsqlexception){
executeInsert:
"
+sqlexception.getMessage());
publicResultSetQuerysimple(Strings){
rs=null;
rs=sql.executeQuery(s);
executeQuery:
returnrs;
publicvoidexecuteDelete(Strings){
try{
//con=DriverManager.getConnection(Connstr);
sql.executeUpdate(s);
executeDelete:
publicintexecuteUpdate(Strings){
executeUpdate:
returni;
}
publicvoidclose(){
if(rs!
=null)
rs.close();
if(con!
con.close();
close:
strConvert代码:
packageapp63;
publicclassStrConvert{
publicStrConvert(){
publicStringchStr(Stringstr){
if(str==null){
str="
else{
str=(newString(str.getBytes("
ISO-8859-1"
),"
GB2312"
)).trim();
catch(Exceptione){
System.out.println("
chStr"
System.out.println(e.getMessage());
returnstr;
(2)视图(message.jsp)截图
单击“增加留言”连接即可进入添加留言页面,如图所示:
Message.jsp的视图代码:
<
%@pagecontentType="
text/html;
charset=gb2312"
%>
%@pageimport="
java.sql.*"
%>
html>
head>
title>
察看留言<
/title>
jsp:
useBeanid="
con"
class="
app63.DBconn"
scope="
request"
/>
body>
tablewidth="
732"
height="
*"
border="
1"
align="
center"
cellpadding="
cellspacing="
0"
bordercolordark="
#ffffff"
bordercolor="
bordercolorlight="
#0066ff"
>
<
trbgcolor="
#CCCCFF"
tdheight="
22"
colspan="
5"
ahref="
addMessage.jsp"
增加留言<
/a>
/td>
/tr>
%
Stringsqli="
SELECT*FROMmasORDERBY留言idDESC"
ResultSetrs=con.executeQuery(sqli);
Stringcolor="
Stringstrpages=(String)request.getParameter("
pages"
intpages=0;
if(strpages==null){
pages=0;
}else{
pages=Integer.parseInt(strpages);
rs.last();
intsqan=4,i=0,fcount=0;
intcount=rs.getRow();
intcountpage=count/sqan;
if(count%sqan>
0)countpage=countpage+1;
if(pages<
0)pages=0;
if(pages>
countpage-1)pages=countpage-1;
fcount=pages*sqan+1;
intfpage=(count-fcount)/sqan+1;
rs.absolute(fcount);
rs.previous();
while(rs.next()){
intid=rs.getInt("
留言id"
Stringusername=rs.getString("
留言人"
Stringtime=rs.getString("
留言时间"
if(masinfo==null)masinfo="
if(color=="
){
color="
}else{
}
%=username%>
%=time%>
%=topic%>
%=color%>
tdalign="
width="
86"
divalign="
left"
留言人:
/div>
158"
&
nbsp;
2"
留言主题:
tdwidth="
298"
日期:
3"
rowspan="
valign="
top"
%=masinfo%>