javaweb利用javabean将数据库中内容遍历在页面输出

效果如下图

javaweb利用javabean将数据库中内容遍历在页面输出_第1张图片

它所实现的就是把数据库中一个表中所有的数据一条一条以表格的形式输出在网页上,

 

javaweb利用javabean将数据库中内容遍历在页面输出_第2张图片

 

 实现方法如下

首先我们要从数据库读取数据,这里要借助javabean来方便我们传递数据

以上面的为例,我要输出课程信息,就要设置好一个课程类,把相应的属性设置好,接下来就要在serverlet中把数据读取进来了

下面是代码:

package serverlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Bean.clas;
import Bean.reportbean;
import DBUtil.DBUtil;

/**
 * Servlet implementation class showclasslet
 */
@WebServlet("/showclasslet")
public class showclasslet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showclasslet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());

		HttpSession session = request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Connection con = null;
                //这里是获取数据库连接
    
		con=DBUtil.getConnection();
                //ArryList为一个动态数组,现在这里它就是一个clas类的数组
		ArrayList list = new ArrayList(); 
		clas x=new clas();
		Statement stmt;
		 ResultSet rsst = null ;
		try {
			
			stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			 rsst = stmt.executeQuery("select * from class where stand='0'");
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		 try {
				while(rsst.next())
				
				{
					 x=new clas();
					//将读取到的数据存入该对象中
					 x.setName(rsst.getString(2));
					x.setPerson(rsst.getString(4));
					x.setTeacher(rsst.getString(3));
					x.setChose(rsst.getString(5));
					x.setHao(rsst.getString(6));
					x.setTname(rsst.getString(8));
					//将赋好值的对象添加入动态数组中
					list.add(x);
					 
					 
					
					 
					 
					 
				}
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			//将动态数组存入session中,方便之后在jsp调用
			 session.setAttribute("list", list);
			 response.sendRedirect("showclass.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
        

  通过上面的操作,已经将数据库中的数据存入了动态数组中,并存储在了session对象"list"中,接下来只要在jsp中将其遍历输出就可以了

这里将其全部输出使用到了标签,具体代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title heretitle>
head>
<style type="text/css">
            /*这里是一些css代码,用来美化表格的,不重要*/            
            table {
                width: 90%;
                background: #ccc;
                margin: 10px auto;
                border-collapse: collapse;    
            }                
            th,td {
                height: 25px;
                line-height: 25px;
                text-align: center;
                border: 1px solid #ccc; 
            }        
            th {
                background: #eee; 
                font-weight: normal;
            }        
            tr {
                background: #fff;
            }        
            tr:hover {
                background: #66FFFF ;
            }        
            td a {
                color: #06f;
                text-decoration: none;
            }        
            td a:hover {
                color: #06f;
                text-decoration: underline;
            }
        style>
<body><% int i=0;
    

%>

 <table class="table table-striped">
  <thead>
    <tr>
      <th>#th>
      <th>名称th>
      <th>编号th>
      <th>教师th>
      <th>总人数th>
      <th>已选人数th>

    tr>
  thead> <tbody>

<c:forEach items="${list}" var="clas">  
    <tr>
      <th scope="row"><%=++i %>th>
      <td><a  href='classinforlet?hao=${clas.hao} ' >${clas.name}a>
      <td>${clas.hao}td>
       <td>${clas.tname}td>
        <td>${clas.person}td>
        <td>${clas.chose}td>
     
      
    tr>
    c:forEach> 

  tbody>
table>

body>
html>

注意一下,要使用标签,jsp开头的“<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>”是必须要有的,这是为了导入相应的标签库。

以上就是全部内容了,感谢阅读。

你可能感兴趣的:(javaweb利用javabean将数据库中内容遍历在页面输出)