javaweb利用javabean將數據庫中內容遍歷在頁面輸出


效果如下圖

它所實現的就是把數據庫中一個表中所有的數據一條一條以表格的形式輸出在網頁上,

 

 

 實現方法如下

首先我們要從數據庫讀取數據,這里要借助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<clas> list = new ArrayList<clas>(); 
		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中將其遍歷輸出就可以了

這里將其全部輸出使用到了<c:forEach>標簽,具體代碼如下:

<%@ 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 here</title>
</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>
<!--這里開始循環輸出list對象中的信息-->
<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>

注意一下,要使用<c:forEach>標簽,jsp開頭的“<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>”是必須要有的,這是為了導入相應的標簽庫。

以上就是全部內容了,感謝閱讀。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM