java web mvc项目实例_JavaWeb开发---MVC案例之查询

本文记录了一次JavaWeb MVC项目的实践过程,主要展示如何通过Servlet、JSP和DAO实现从MySQL数据库查询并显示所有学生信息。步骤包括创建Servlet、JavaBean、DAO,配置JDBC连接,以及展示结果的JSP页面。
摘要由CSDN通过智能技术生成

现在Javaweb开发第一阶段学习步入实践阶段,在这儿将自己做的项目记下来,也方便积累经验。

下图是处理流程:

0b921f42f1a8dc98d6ba8b7a72b9a691.png

MySQL数据库表如下所示:

9713e31016c680ab169e9e0e506eb503.png

1.在web项目webContent中新建test.jsp

pageEncoding="UTF-8"%>

Insert title here

List All Students

2在Java-src中新建ListAllStudentServlet.java,并对其进行配置

package com.javaweb.mvc;

import java.io.IOException;

import java.util.Arrays;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ListAllStudentsServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

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

StudentDao studentDao = new StudentDao();

List students = studentDao.getAll();

request.setAttribute("students", students);

request.getRequestDispatcher("/students.jsp").forward(request, response);

}

}

3.继续创建Student.java

package com.javaweb.mvc;

public class Student {

private Integer id;

private String studentName;

private String location;

private String telephone;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

this.studentName = studentName;

}

public String getLocation() {

return location;

}

public void setLocation(String location) {

this.location = location;

}

public String getTelephone() {

return telephone;

}

public void setTelephone(String telephone) {

this.telephone = telephone;

}

public Student(Integer id, String studentName, String location, String telephone) {

super();

this.id = id;

this.studentName = studentName;

this.location = location;

this.telephone = telephone;

}

}

4.创建StudentDao.java

package com.javaweb.mvc;

import java.util.ArrayList;

import java.util.List;

import java.sql.*;

public class StudentDao {

public List getAll(){

List students = new ArrayList();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/student?user=root&password=1234";

String user = "root";

String password = "1234";

String sql = "select * from stu";

conn = DriverManager.getConnection(url, user, password);

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

while(rs.next()){

int id = rs.getInt(1);

String studentName = rs.getString(2);

String location = rs.getString(3);

String telephone = rs.getString(4);

Student student = new Student(id, studentName, location, telephone);

students.add(student);

}

}catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

if(rs != null)

rs.close();

if(ps != null)

ps.close();

if(conn != null)

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return students;

}

}

5.在webContent中新建students.jsp

pageEncoding="UTF-8"%>

Insert title here

List students = (List)request.getAttribute("students");

%>

idstudentNamelocationtelephone

for(Student student:students){

%>

}

%>

6.添加JDBC驱动

添加在lib文件夹,因为lib文件夹中存放支持web应用运行的JAR文件

3e858eee09ec39e6194263b534c21f88.png

7.接下来就可以运行了

这是运行结果:

c88163150de771ff8d7988b5f9aaab62.png

JavaWeb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值