编写程序:使用MVC模式开发程序,完成数据的模糊查询。
要求:
(1)index.jsp用于输入要查询的数据。
(2)result.jsp:用于显示结果。
(3)queryServlet:用于验证数据、实例化JavaBean、调用连接数据库、控制页面跳转
(4)queryDAO:用于连接数据库及进行数据库的操作如:查询、删除、更改等
(5)Student:JavaBean用于数据的封装,方便将查询结果在servlet与jsp页面之间进行传递等
以上几个部分共同构成了MVC模式,JSP为MVC模式当中的V,Servlet为C,queryDAO与JavaBean合在一起为M。
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
请输入要查询的内容:
<%@ page language="java" import="java.util.*,com.mars.*" pageEncoding="gb2312"%>
查询结果
查询结果如下:
学号
姓名
年龄
性别
地址
<%
ArrayList arrayList = (ArrayList)request.getAttribute("arrayList");
for(int i=0; i
<%=student.getId()%>
<%=student.getName()%>
<%=student.getAge()%>
<%=student.getSex()%>
<%=student.getAddress()%>
<%
}
%>
index.jsp
queryServlet
com.mars.queryServlet
queryServlet
/queryServlet
package com.mars;
public class Student {
private int id;
private String name;
private String sex;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.mars;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mars.QueryDAO;
public class queryServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String name = request.getParameter("name");
QueryDAO qd = new QueryDAO();
ArrayList arrayList = qd.queryLike(name);
request.setAttribute("arrayList", arrayList);
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
package com.mars;
import java.sql.*;
import java.util.ArrayList;
import com.mars.Student;
public class QueryDAO {
private Connection conn = null;
public QueryDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testmvc", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public ArrayList queryLike(String name) {
ArrayList arrayList = new ArrayList();
String SQL = "select * from student where name like" + "'%" + name
+ "%'" + "or address like " + "'%" + name + "%'";
try {
ResultSet resultSet = conn.createStatement().executeQuery(SQL);
while (resultSet.next()) {
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
student.setSex(resultSet.getString("sex"));
student.setAddress(resultSet.getString("address"));
arrayList.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
}
drop database testmvc;
create database testmvc;
use testmvc;
create table student (
id int not null primary key auto_increment,
name varchar(10),
age int,
sex varchar(2),
address varchar(50)
);
insert into student values(null,'张三',20,'男','北京市东城区天安门');
insert into student values(null,'李四',21,'男','上海市东城区天安门');
insert into student values(null,'王五',22,'男','天津市东城区天安门');
insert into student values(null,'赵六',23,'男','南京市东城区天安门');
insert into student values(null,'孙七',24,'男','福建省东城区天安门');
insert into student values(null,'钱八',25,'男','长沙市东城区天安门');
insert into student values(null,'刘九',26,'男','武汉市东城区天安门');