实现建立一个学生的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="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设计模式下实现数据库的连接,并获取所有数据到浏览器页面上显示的更多相关文章

  1. MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

    MVC模式(Model View Controller): Model:DAO模型 View:JSP  在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...

  2. mvc在页面上显示PDF

    今天看到需求要在页面上显示pdf,自己整了半天,啥效果都没有,偶尔有效果还各种不兼容,很无语的说.捣鼓了半天,没办法了,去谷歌了下,介绍了各种插件,各种方法,但是都挺繁琐的,本人不是一个很喜欢使用插件 ...

  3. Unary模式下客户端从开始连接到发送接收数据的主要流程

    (原创)C/C/1.25.0-dev grpc-c/8.0.0, 使用的例子是自带的例子GreeterClient grpc Unary模式下客户端从开始连接到发送数据的主要流程 graph TD; ...

  4. 允许Ubuntu系统下Mysql数据库远程连接

    第一步: vim /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1 或者改为: bind-ad ...

  5. 关于MVC设计模式下的Model

    内容1: 1.大多数情况下,会有两个关于Model的文件. 一个称他为Entity Model,他里面的字段一般是与数据库直接交互的,也就是说,Entity里面每一个字段赋予的属性都是对应着数据库来的 ...

  6. Spring MVC框架下 将数据库内容前台页面显示完整版【获取数据库人员参与的事件列表】

    1.书写jsp页面包括要显示的内容[people.jsp] <!-- 此处包括三个方面内容: 1.包含 文本输入框 查询按钮  查询结果显示位置 (paging) 2.包括对按钮(button) ...

  7. xshell下mysql数据库只导出表结构不导出数据

    操作系统:linux: 使用软件:xshell.winscp 进入系统之后输入命令: mysqldump -u 用户名 - p 密码 -d 数据库名 > aaa.sql 注意字符间的空格. 之后 ...

  8. [工作相关] GS产品使用LInux下Oracle数据库以及ASM存储时的数据文件路径写法.

    1. 自从公司的GS5版本就已经支持Linux下的oracle数据库通过安装工具自动安装注册了, 只不过路径需要使用linux的命名规则, 如图: /home/oracle/ 注意 最后是有一个 斜线 ...

  9. mysql命令行下创建数据库,创建表,插入数据,查询数据

    1.创建数据库 mysql> create DATABASE booktik -> ;Query OK, 1 row affected (0.02 sec) 2.创建表 mysql> ...

随机推荐

  1. ios app下载跳到itunes

    <body class="box"> <div class="text"> <a href="https://itune ...

  2. C++Primer 第二章

    //1.程序尽量避免依赖于实现环境的行为.比如:如果将int的尺寸看成一个确定不变的已知值,那么这样的程序就称为不可移植的. typedef int int32; //使用类似的typedef,可以有 ...

  3. 树形DP(01组合背包The Ghost Blows Light HDU4276)

    题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...

  4. [分享]关于windows下的小技巧

    ----1.首先,决定您要增加到菜单中的文件类型,以及启动这类文件的应用程序.如果是某些在启动时会自动打开的新文件或让您可以立即使用的应用程序,如记事本.写字板或画图等,就不需要特别的准备工作.但如果 ...

  5. cookie的保存时间

    使用 .setMaxAge()设置(单位是秒) second>0 保存在硬盘上的时间 second<0 默认保存在浏览器的内存中 second=0 立即删除

  6. BZOJ K大数查询(分治)(Zjoi2013)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3110 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b ...

  7. 2.session与cookie的区别?

    session:储存用户访问的全局唯一变量,存储在服务器上的php指定的目录中的(session_dir)的位置进行的存放 cookie:用来存储连续訪問一个頁面时所使用,是存储在客户端,对于Cook ...

  8. form文件上传,防止页面刷新

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>文件上传 ...

  9. sql 中各种锁随记

    一. 为什么要引入锁    多个用户同时对数据库的并发操作时会带来以下数据不一致的问题:    丢失更新  A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系 ...

  10. sql 索引创建

    --格式 --CREATE [索引类型] INDEX 索引名称--ON 表名(列名)--WITH FILLFACTOR = 填充因子值0~100--GO ----------------------- ...