基于MVC的简单数据库系统管理Word下载.docx
《基于MVC的简单数据库系统管理Word下载.docx》由会员分享,可在线阅读,更多相关《基于MVC的简单数据库系统管理Word下载.docx(12页珍藏版)》请在冰豆网上搜索。
![基于MVC的简单数据库系统管理Word下载.docx](https://file1.bdocx.com/fileroot1/2023-1/8/d83d94cc-c0b9-4e71-9e8c-7220bff6e611/d83d94cc-c0b9-4e71-9e8c-7220bff6e6111.gif)
如果有查询到记录,则显示响应的结果。
NoResult.jsp:
如果没有查询到记录,显示提示信息。
(1)QueryForm.jsp:
该JSP页面只提供form表单。
注意:
action的配置要跟web.xml中的配置一致。
A)新建JSP文件QueryForm.jsp,编写表单
(2)QueryResult.jsp:
显示结果
A)用jsp:
useBean创建StudentBean对象stu。
B)从Servlet转发过来的request对象中获取查询结果,并用jsp:
setPropertystu设置StudentBean对象stu的成员变量。
C)使用jsp:
getProperty获取每个属性值,并显示结果
(3)NoResult.jsp:
主要显示提示信息
4.控制器Servlet的设计
5.QueryServlet.java功能:
接收用户的查询请求,并根据用户输入的姓名调用StudentDAO类查询数据库。
根据查询结果转至不同的页面.
(1)在包中创建servlet,名称为QueryServlet.java。
(2)编写doGet()方法
A)获取用户输入的参数名
B)调用JavaBean查询数据库
C)如果能够查询到满足条件的记录,则把结果存放到request范围内,并转发至QueryResult.jsp页面;
否则转发至NoResult.jsp页面。
5.web.xml的配置
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
web-appversion="
2.4"
xmlns="
xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
<
servlet>
description>
ThisisthedescriptionofmyJ2EEcomponent<
/description>
display-name>
ThisisthedisplaynameofmyJ2EEcomponent<
/display-name>
servlet-name>
QueryServlet<
/servlet-name>
servlet-class>
ch.QueryServlet<
/servlet-class>
/servlet>
servlet-mapping>
url-pattern>
/Query<
/url-pattern>
/servlet-mapping>
welcome-file-list>
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
/web-app>
5、实验结果
源代码:
JAVABEAN:
//StudentBean.java
packagech;
publicclassStudentBean{
privateStringstuId;
privateStringstuName;
privateStringstuSex;
privateStringstuBir;
privateStringstuAdd;
publicStringgetStuId(){
returnstuId;
}
publicvoidsetStuId(StringstuId){
this.stuId=stuId;
publicStringgetStuName(){
returnstuName;
publicvoidsetStuName(StringstuName){
this.stuName=stuName;
publicStringgetStuSex(){
returnstuSex;
publicvoidsetStuSex(StringstuSex){
this.stuSex=stuSex;
publicStringgetStuBir(){
returnstuBir;
publicvoidsetStuBir(StringstuBir){
this.stuBir=stuBir;
publicStringgetStuAdd(){
returnstuAdd;
publicvoidsetStuAdd(StringstuAdd){
this.stuAdd=stuAdd;
}
JSP:
//TestRectangle.jsp
%@pagelanguage="
java"
import="
java.util.*"
pageEncoding="
gbk"
%>
html>
head>
title>
MyJSP'
TestRectangle.jsp'
startingpage<
/title>
/head>
body>
ThisismyJSPpage.<
br>
formaction="
Test.jsp"
method="
post"
请输入长:
inputtype="
text"
name="
a"
/>
请输入宽:
b"
submit"
value="
提交"
/form>
/body>
/html>
//StudentDAO
importjava.sql.*;
publicclassStudentDAO{
privateConnectionconn=null;
publicvoidinitConnection(){
try{
Class.forName("
com.mysql.jdbc.Driver"
);
DriverManager.registerDriver(newcom.mysql.jdbc.Driver());
conn=DriverManager.getConnection("
jdbc:
mysql:
//localhost:
3306/school"
"
root"
jttqzh"
catch(Exceptionex)
{ex.printStackTrace();
publicStudentBeanqueryStuByName(Stringsname){
StudentBeanstus=newStudentBean();
try{
System.out.println("
sname1:
"
+sname);
sname=newString(sname.getBytes("
ISO-8859-1"
),"
gb2312"
Stringsql="
SELECT*FROMstudentWHEREname='
"
+sname+"
'
;
initConnection();
ResultSetrs=conn.createStatement().executeQuery(sql);
while(rs.next()){
stus.setStuId(rs.getString
(1));
System.out.println(rs.getString
(1));
stus.setStuName(rs.getString
(2));
System.out.println(rs.getString
(2));
stus.setStuSex(rs.getString(3));
System.out.println(rs.getString(3));
stus.setStuBir(rs.getString(4));
System.out.println(rs.getString(4));
stus.setStuAdd(rs.getString(5));
System.out.println(rs.getString(5));
this.closeConnection();
catch(Exceptione){e.printStackTrace();
returnstus;
publicvoidcloseConnection(){
try{if(conn!
=null){
conn.close();
}}catch(Exceptione){}
Servlet:
//QueryServlet.java
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassQueryServletextendsHttpServlet{
publicQueryServlet(){
super();
publicvoiddestroy(){
super.destroy();
//Justputs"
destroy"
stringinlog
//Putyourcodehere
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringsname=request.getParameter("
sName"
StudentDAOstudentDAO=newStudentDAO();
StudentBeanstus=studentDAO.queryStuByName(sname);
if(stus.getStuId()!
request.setAttribute("
stus"
stus);
ServletContextapplication=this.getServletContext();
RequestDispatcherrd=application.getRequestDispatcher("
/QueryResult.jsp"
rd.forward(request,response);
}
else{
/NoResult.jsp"
response.setContentType("
text/html"
PrintWriterout=response.getWriter();
out
.println("
!
DOCTYPEHTMLPUBLIC\"
-//W3C//DTDHTML4.01Transitional//EN\"
out.println("
HTML>
HEAD>
TITLE>
AServlet<
/TITLE>
/HEAD>
BODY>
out.print("
Thisis"
out.print(this.getClass());
usingtheGETmethod"
/BODY>
/HTML>
out.flush();
out.close();
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
usingthePOSTmethod"
publicvoidinit()throwsServletException{
JSP
//QueryForm.jsp
QueryForm.jsp'
Query"
get"
请输入姓名:
查询"
//QueryResult.jsp
jsp:
useBeanid="
stu"
class="
ch.StudentBean"
scope="
request"
/jsp:
useBean>
setPropertyname="
property="
stuId"
%=((StudentBean)request.getAttribute("
)).getStuId()%>
stuName"
)).getStuName()%>
stuSex"
)).getStuSex()%>
stuBir"
)).getStuBir()%>
stuAdd"
)).getStuAdd()%>
table>
tr>
td>
学号:
/td>
getPropertyname="
/tr>
姓名:
性别:
出生日期:
住址:
/table>
//NoResult.jsp
Wrong<
divalign="
center"
fontcolor=redsize="
5"
WRONG<
/font>
/div>
截图:
6、实验心得
掌握jsp、servlet、JavaBean之间的关联