JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx

上传人:b****5 文档编号:19501749 上传时间:2023-01-07 格式:DOCX 页数:15 大小:480.58KB
下载 相关 举报
JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx_第1页
第1页 / 共15页
JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx_第2页
第2页 / 共15页
JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx_第3页
第3页 / 共15页
JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx_第4页
第4页 / 共15页
JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx

《JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。

JavaWeb用MV模式C实现简单的图书管理系统报告资料Word格式文档下载.docx

//图书书名

privateStringauthor=null;

//图书作者

privateStringpublisher=null;

//图书出版社

privatefloatprice=0。

0F;

//图书价格

publicBookBean(){}

publicBookBean(StringbookId,Stringauthor,

Stringtitle,Stringpublisher,floatprice){

this.bookid=bookId;

this。

title=title;

author=author;

this.publisher=publisher;

this.price=price;

}

publicStringgetBookid(){returnthis.bookid;

publicStringgetTitle(){returntitle;

publicStringgetAuthor(){returnthis。

author;

publicfloatgetPrice(){returnprice;

publicStringgetPublisher(){returnpublisher;

publicvoidsetBookid(Stringbookid){this。

bookid=bookid;

publicvoidsetTitle(Stringtitle){this。

title=title;

publicvoidsetAuthor(Stringauthor){this.author=author;

publicvoidsetPrice(floatprice){this。

price=price;

publicvoidsetPublisher(Stringpublisher){this.publisher=publisher;

}

(2)BookDAO是一个简单的JavaBeans,它实现数据库的访问

packagecom.beans;

importjava.sql。

*;

importjavax.sql。

importjavax。

naming。

*;

importjava.util。

ArrayList;

publicclassBookDAO{

privatestaticContextcontext=null;

privateDataSourcedataSource=null;

publicBookDAO(){

try{

if(context==null){

context=newInitialContext();

context=(Context)context。

lookup(“java:

comp/env"

);

//连接的是在context。

xml下面配置的数据库连接池

dataSource=(DataSource)context。

lookup(”databasePool”);

}catch(NamingExceptione2){

//根据书号查询图书信息

publicBookBeansearchBook(Stringbookid){

Connectionconn=null;

PreparedStatementpstmt=null;

ResultSetrst=null;

BookBeanbook=newBookBean();

try{

conn=dataSource。

getConnection();

pstmt=conn.prepareStatement("

SELECT*FROMbooksWHEREbookid=?

”);

pstmt.setString(1,bookid);

rst=pstmt。

executeQuery();

if(rst.next()){

book。

setBookid(rst.getString("

bookid"

));

book.setTitle(rst.getString("

title”));

book。

setAuthor(rst。

getString(”author"

book.setPublisher(rst.getString(”publisher”));

setPrice(rst.getFloat("

price"

));

returnbook;

}else{

returnnull;

}catch(SQLExceptionse){

returnnull;

}finally{

try{

conn.close();

}catch(SQLExceptionse){

}

}

//插入一本图书记录

publicbooleaninsertBook(BookBeanbook){

Connectionconn=null;

PreparedStatementpstmt=null;

try{

conn=dataSource.getConnection();

pstmt=conn.prepareStatement(

”INSERTINTObooksVALUES(?

,?

,?

)”);

pstmt。

setString(1,book。

getBookid());

pstmt.setString(2,book.getTitle());

setString(3,book。

getAuthor());

pstmt.setString(4,book.getPublisher());

pstmt.setFloat(5,book。

getPrice());

pstmt.executeUpdate();

pstmt.close();

returntrue;

}catch(SQLExceptionse){

returnfalse;

conn。

close();

}catch(SQLExceptionse){}

(3)JSP页面bookQuery.jsp实现根据书号查询图书信息

〈%@pagecontentType="

text/html;

charset=UTF—8"

%〉

〈html〉<

head〉<

title>

BookQuery<

/title>

〈/head>

<

body>

请输入一个书号:

br〉

formaction="

bookquery.do"

method="

post”〉

inputtype=”text”name=”bookid”〉<

br>

inputtype=”submit"

value="

提交”〉

〈/form〉

/body>

/html>

(4)JSP页面bookInsert。

jsp实现向数据库中插入数据

〈%@pagecontentType="

text/html;

charset=UTF-8”%>

html>

〈head〉<

BookInsert〈/title>

〈/head〉

〈h3〉请输入图书信息:

/h3〉

%if(request.getAttribute(”result"

)!

=null)

out.print(request。

getAttribute("

result”));

%〉

〈formaction=”bookinsert。

do"

<

table>

tr>

td>

书号〈/td>

〈td>

inputtype="

text”name=”bookid"

>

/td>

/tr>

td〉书名<

〈td〉〈inputtype="

text"

name="

title”>

tr〉〈td>

作者〈/td>

td〉<

name=”author”>

/td〉<

/tr〉

〈td〉出版社<

〈td>

〈inputtype="

text”name=”publisher"

tr〉<

td〉单价〈/td>

inputtype=”text”name="

price”〉<

〈/tr〉

〈tr〉<

inputtype=”submit”value=”确定"

〈/td〉

〈td〉<

inputtype=”reset"

重置”>

/td〉

/table〉

/html〉

(5)显示查询结果的JSP页面display。

jsp

charset=UTF-8”%>

〈jsp:

useBeanid=”book”class=”com.beans.BookBean"

scope="

session”/〉

书号:

〈jsp:

getPropertyname="

book”property="

/>

书名:

getPropertyname=”book"

property="

title”/〉

作者:

book"

property=”author"

出版社:

publisher"

价格:

jsp:

price”/〉

〈/body〉〈/html〉

(6)错误页面errorPage.jsp

〈%@pagecontentType="

charset=UTF—8"

〈html>

body〉

对不起,您查的图书不存在!

〈/body〉〈/html>

(7)从数据库中查找该书,最后根据查询结果将请求转发到显示页面(display。

jsp)或错误页面(errorPage。

jsp)

packagecom。

control;

*;

importjava.sql.*;

servlet。

importjavax.servlet。

http。

importcom.beans.BookBean;

importcom。

beans。

BookDAO;

publicclassBookQueryServletextendsHttpServlet{

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

Stringbookid=request.getParameter(”bookid"

BookDAObookdao=newBookDAO();

BookBeanbook=bookdao。

searchBook(bookid);

if(book!

=null){

request。

getSession().setAttribute(”book”,book);

RequestDispatcherview=request。

getRequestDispatcher(”/display。

jsp"

);

view.forward(request,response);

getRequestDispatcher(”/errorPage。

jsp”);

view。

forward(request,response);

(8)Servlet实现向数据库插入数据,并将控制请求的转发到bookInsert。

jsp页面。

packagecom.control;

importjava.io.*;

sql.*;

servlet.*;

http.*;

BookBean;

publicclassBookInsertServletextendsHttpServlet{

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

setCharacterEncoding("

UTF—8”);

Stringmessage=null;

BookBeanbook=newBookBean(

request.getParameter(”bookid”),request。

getParameter(”title"

),

request.getParameter("

author"

),request.getParameter(”publisher"

Float.parseFloat(request。

getParameter("

price”))

);

BookDAObookdao=newBookDAO();

booleansuccess=bookdao.insertBook(book);

if(success){

message="

成功插入一条记录!

”;

}else{

插入记录错误!

"

;

setAttribute("

result”,message);

getRequestDispatcher("

/bookInsert。

forward(request,response);

(9)配置context文件

xmlversion=”1。

0”encoding=”UTF—8"

>

Context>

Resourcename="

databasePool"

type="

javax。

sql.DataSource”

auth=”Container"

driverClassName="

com。

mysql。

jdbc。

Driver”

url="

jdbc:

mysql:

//localhost:

3306/library?

useUnicode=true&amp;

characterEncoding=UTF-8"

username=”root"

password=”zzhao"

maxTotal=”8"

maxIdle=”2"

maxWaitMillis=”60000”/〉

/Context〉

(10)在web。

xml文件中部署Servlet和context资源

?

0"

encoding=”UTF—8”?

web-appxmlns:

xsi="

http:

//www.w3。

org/2001/XMLSchema-instance”xmlns=”http:

//java。

xsi:

schemaLocation="

http:

http:

id=”WebApp_ID"

version=”3.0"

display—name>

SimpleBookManager〈/display—name〉

welcome-file—list>

welcome—file〉index.html<

/welcome-file〉

〈welcome—file>

index。

htm<

/welcome—file>

〈welcome-file〉index.jsp<

/welcome-file>

welcome-file〉default.html<

〈welcome-file>

default.htm〈/welcome-file>

default。

jsp〈/welcome-file>

〈/welcome-file—list〉

resource-ref>

〈res—ref—name>

databasePool<

/res—ref-name>

〈res-type>

javax.sql.DataSource〈/res-type>

〈res-auth〉Container〈/res-auth〉

〈/resource—ref>

servlet>

<

servlet-name>

bookQuery<

/servlet-name〉

servlet—class>

control。

BookQueryServlet〈/servlet—class>

〈/servlet〉

servlet-mapping>

〈servlet-name〉bookQuery〈/servlet—name〉

url—pattern〉/bookquery.do<

/url—pattern>

〈/servlet—mapping〉

〈servlet〉

servlet—name〉bookInsert〈/servlet—name>

〈servlet-class>

BookInsertServlet〈/servlet—class〉

〈/servlet>

〈servlet-mapping>

〈servlet-name>

bookInsert〈/servlet—name>

〈url-pattern>

/bookinsert.do<

/url-pattern〉

〈/servlet—mapping>

〈/web—app>

4.测试与结果

首页

查询操作

当数据库中有这个图书的编号时

当数据库中没有这个图书的编号时

插入操作

查询刚才插入的书,是否已经保存到数据库中

四、感想和总结

这次实验,给自己一个机会系统的复习下自己以前学习的知识,加深对MVC框架的理解,以及对数据库的操作,再者就是Tomcat环境下的数据库链接池的使用,如何配置使用等,还有在试验中遇到了字符编码插入数据库时乱码的问题,在扩展了自己对字符编码乱码的处理。

总的来说,是学习到了一些东西。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 卡通动漫

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1