JSP+Servlet+MySQL完成将数据库中的数据显示到页面

步骤:

1 先写Servlet代码,首先导入mysql-connector-java-5.1.40-bin.jar,在init()方法中加载jdbc驱动,在doGet()方法中获取数据库连接对象,然后获取SQL的操作对象。

代码如下:

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.User;

public class JdbcJspServlet extends HttpServlet {
	
	@Override
	public void init() throws ServletException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			List<User> userList = new ArrayList<>();
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","123456");
			ps = conn.prepareStatement("select * from tb_user");
			rs = ps.executeQuery();
			while(rs.next()){
				String name = rs.getString("userName");
				String password = rs.getString("userPwd");
				User user = new User(name, password);
				userList.add(user);
			}
			req.setAttribute("userList", userList);
			req.getRequestDispatcher("home.jsp").forward(req, resp);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null){
					rs.close();
				}
				if(ps!=null){
					ps.close();
				}
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

2 在1中要用到一个实体类User,所以要写一个User类,把查询的结果放到一个ArrayList集合中。

代码如下:

import java.io.Serializable;

public class User implements Serializable {
	private String name;
	private String password;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String name, String password) {
		super();
		this.name = name;
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

3 配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Day42_jdbc_jsp_servlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>JdbcJspServlet</servlet-name>
  	<servlet-class>com.jdbcjspservlet.JdbcJspServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>JdbcJspServlet</servlet-name>
  	<url-pattern>/jdbcJspServlet.do</url-pattern>
  </servlet-mapping>
</web-app>

4 写要将数据显示的jsp页面

<%@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>
	<ul>
	<%
	Object obj = request.getAttribute("userList");
	List<User> userList = null;
	if(obj instanceof List){
		userList = (List<User>)obj;
	}
	if(userList!=null){
		for(User user:userList){
			String userinfo = user.getName()+"---"+user.getPassword();
			%>
			<li><%=userinfo %></li>
			<%
		}
	}
	%>
	</ul>
</body>
</html>

5 启动TomCat服务器,在浏览器地址栏输入地址即可将查询到的数据库数据显示到网页。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31462011/article/details/70244512
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭