您的位置:首页 > 运维架构 > 网站架构

基于MVC架构的Web学生信息查询

2011-04-07 11:51 531 查看
一、功能要求

1、输入学生姓名,查询并输出全部同名学生信息;
2、涉及到多记录集合信息的输出;
3、视图层不再使用繁琐的jsp:useBean ,也不包括任何Java代码;
4、代码直观简洁。

5、不存在中文乱码问题

6、开发环境:Tomcat7.0 、MySQL5.5、Eclipse3.6、Navicat9.1

7、系统支持:http://tomcat.apache.org/taglibs/standard/去下载Standard 1.1 ,将jstl.jar、standard.jar放到项目WEB-INF/lib下;

二、MySQL数据表student

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stuno` char(10) NOT NULL DEFAULT '' COMMENT '学号',
`stuname` varchar(20) DEFAULT NULL COMMENT '姓名',
`sex` char(2) DEFAULT NULL COMMENT '性别',
`classname` varchar(30) DEFAULT NULL COMMENT '班级',
`tel` varchar(20) DEFAULT NULL COMMENT '电话',
PRIMARY KEY (`id`,`stuno`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '001', '张三', '男', '国贸001', '87655123');
INSERT INTO `student` VALUES ('2', '002', '李四', '男', '国贸002', '87600456');
INSERT INTO `student` VALUES ('3', '1001', '王五', '男', '信管101', '88661234');
INSERT INTO `student` VALUES ('4', '1003', '张东', '男', '信管103', '88765432');
INSERT INTO `student` VALUES ('6', '00501', '张三', '男', '电商051', '12345657');
INSERT INTO `student` VALUES ('7', '006103', '李四', '男', '电商062', '12345666');
INSERT INTO `student` VALUES ('8', '008107', '顽固', '男', '工管083', '12345605');
INSERT INTO `student` VALUES ('9', '1006', '李四', '男', '工管101', '12345694');

三、模型层

1)Student类:代表学生对象

public class Student {
private int id;
private String stuno;
private String stuname;
private String sex;
private String classname;
private String tel;

public Student() {
super();
}

public Student(int id, String stuno, String stuname, String sex,
String classname, String tel) {
this.id = id;
this.stuno = stuno;
this.stuname = stuname;
this.sex = sex;
this.classname = classname;
this.tel = tel;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getStuno() {
return stuno;
}

public void setStuno(String stuno) {
this.stuno = stuno;
}

public String getStuname() {
return stuname;
}

public void setStuname(String stuname) {
this.stuname = stuname;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getClassname() {
return classname;
}

public void setClassname(String classname) {
this.classname = classname;
}

public String getTel() {
return tel;
}

public void setTel(String tel) {
this.tel = tel;
}

}


2)StudentDAO类:用于操作学生对象

public class StudentDAO {

public List<Student> getStudent(String stuname) {
List<Student> list = null;
try {
String sql = "select * from student where stuame=?";
DBManager db = new DBManager(sql);
PreparedStatement ps = db.getPreparedStatement();
ps.setString(1, stuame);
ResultSet rs = ps.executeQuery();
list = new ArrayList<Student>();
while (rs.next()) {
int id = rs.getInt("id");
String stuno = rs.getString("stuno");
String stuname = rs.getString("stuname");
String sex = rs.getString("sex");
String classname = rs.getString("classname");
String tel = rs.getString("tel");
Student student = new Student(id, stuno, stuname, sex, classname,tel);
list.add(student);
}
ps.close();
db.closeConn();
} catch (SQLException e) {  e.printStackTrace(); }
return list;
}
}


3)DBManager类,负责连接数据库

public class DBManager {
private Connection conn = null;
private static final String url = "jdbc:mysql://localhost/mytest?useUnicode=true&characterEncoding=UTF-8";
private static final String user = "root";
private static final String password = "007";
private PreparedStatement preparedStatement = null;

public DBManager(String sql) {
try {
conn = DriverManager.getConnection(url, user, password);
if (conn == null)
return;
preparedStatement = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}

public void closeConn() {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public PreparedStatement getPreparedStatement() {
return preparedStatement;
}
}


四、控制层

1)QueryAction类:查询控制器,是一个Servlet类,这里只列出其方法代码

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);

}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String studentname = request.getParameter("studentname");
if (studentname == null) {// 未Post查询直接进入
request.setAttribute("queryresult", null);
} else {// Post后查询
StudentDAO studao = new StudentDAO();
List<Student> student = studao.getStudent(studentname);
request.setAttribute("queryresult", student);
}
RequestDispatcher rd = request
.getRequestDispatcher("query.jsp");
rd.forward(request, response);
}


五、视图层

1)query.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form name="login" method="post" action="queryAction">
待查询学生姓名<input	type="text" name="studentname">
<input type="submit" value="查询">
<input type="reset" value="重置">
</form>
<c:if test="${queryresult!=null&&empty queryresult}">
<font style="color: #FF0000" mce_style="color: #FF0000">查无此人!</font>
</c:if>
<c:if test="${!empty queryresult}">
<table border=1 align=center width=300>
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>班级</td>
<td>电话</td>
</tr>
<c:forEach items="${queryresult}" var="student">
<tr>
<td>${student.stuno}</td>
<td>${student.stuname}</td>
<td>${student.sex}</td>
<td>${student.classname}</td>
<td>${student.tel}</td>
</tr>
</c:forEach>
</table>
</c:if>
</center>
</body>
</html>


六、运行效果



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐