首页 > 代码库 > MVC设计模式下实现数据库的连接,并获取所有数据到浏览器页面上显示
MVC设计模式下实现数据库的连接,并获取所有数据到浏览器页面上显示
实现建立一个学生的java类:里面封装了属性的全部属性;
public class Student {
private int id;
private String username;
private String password;
public Student() {
super();
}
public Student(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
-------------------------------------------------------------------------------------------------
建立一个java类;实现数据库的连接,并实现查询功能,查询到所有属性的所有值,用返回一个list集合承接;
public class StudentDAO {
public List<Student> getAll(){
List<Student> list=new ArrayList<Student>();
//该方法获取连接数据库的方法
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
String sql="select id,username,password from person";
try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql:///test";
String user="root";
String password="lxn123";
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password1=resultSet.getString(3);
Student student=new Student(id,username,password1);
list.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
}
-------------------------------------------------------------------------------------------------
建立一个Servlet类,实现从类StudentDAO里面的getAll方法里获取到数据库里面的全部信息,和请求的转发的功能
public class ListAllStudent extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StudentDAO studentDAO=new StudentDAO();
List<Student> students=studentDAO.getAll();
request.setAttribute("student", students);
//请求的转发的地址
request.getRequestDispatcher("/students.jsp").forward(request, response);
}
}
------------------------------------------------------------------------------------------------
请求转发的地址在这儿students.jsp:是一个jsp文件
<%@page import="com.lanqiao.javatest.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Student> names=(List<Student>)request.getAttribute("student");
%>
<table border="1">
<tr>
<th>Id</th>
<th>userName</th>
<th>password</th>
</tr>
<%for(Student list:names){%>
<tr>
<td><%=list.getId()%></td>
<td><%=list.getUsername()%></td>
<td><%=list.getPassword()%></td>
<tr>
<%}%>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------
是一个jsp文件,test.jsp,是一个超链接,其内容显示在浏览器页面上,点击可实现这个工程要求的功能
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="http://www.mamicode.com/listAllStudent">List All Student Page</a>
</body>
</html>
点击超链接前:
点击超链接后:
数据库中的源文件为:
--------------------------------------------------------------------------------------------------
在lib下面web.xml文件内容为:里面设置和反射获取信息;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>
<display-name>ListAllStudent</display-name>
<servlet-name>ListAllStudent</servlet-name>
<servlet-class>com.lanqiao.javatest.ListAllStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListAllStudent</servlet-name>
<url-pattern>/listAllStudent</url-pattern>
</servlet-mapping>
</web-app>
MVC设计模式下实现数据库的连接,并获取所有数据到浏览器页面上显示