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 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文件:



  Day42_jdbc_jsp_servlet
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  	JdbcJspServlet
  	com.jdbcjspservlet.JdbcJspServlet
  
  
  	JdbcJspServlet
  	/jdbcJspServlet.do
  

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

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


	
    <% Object obj = request.getAttribute("userList"); List userList = null; if(obj instanceof List){ userList = (List)obj; } if(userList!=null){ for(User user:userList){ String userinfo = user.getName()+"---"+user.getPassword(); %>
  • <%=userinfo %>
  • <% } } %>

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

你可能感兴趣的:(JSP+Servlet+MySQL完成将数据库中的数据显示到页面)