JSP+Servlet+JavaBean+JDBC显示数据列表

参考文章
第一个JSP+Servlet+JavaBean+JDBC示例程序

jsp学习日记(一)javabean的使用和JavaBean+servlet的

JSP+JavaBean+Servlet+MSSQLServer2000实现分页查询


Servlet
package servlet;

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 javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.AppInfo;

public class WServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		//设置HTTP响应的文档类型,此处为Text/html,如果更改为application\msword则设置为word文档格式  
        response.setContentType("text/html");  
        //设置响应所采用的编码方式  
        response.setCharacterEncoding("GB18030");
        
        ArrayList<AppInfo> appInfoList = getAppInfoList();
        request.setAttribute("appInfoList", appInfoList);
        RequestDispatcher rd=request.getRequestDispatcher("show_app_info.jsp");  
        rd.forward(request,response);  
	}
	
	private ArrayList<AppInfo> getAppInfoList(){
		ArrayList<AppInfo> appInfoList = new ArrayList<AppInfo>();
		Connection conn=null;  
	    PreparedStatement ps=null;  
	    ResultSet rs=null;  
	    // 1. 注册驱动 
	    try { 
	        Class.forName("com.mysql.jdbc.Driver"); 
	    } catch(ClassNotFoundException ex) { 
	        ex.printStackTrace(); 
	    } 
	    
	    try { 
	        //2. 获取数据库的连接 
	        conn = DriverManager.getConnection 
	            ("jdbc:mysql://localhost:3306/app_info?" +
	            		"useUnicode=true&characterEncoding=GBK","cc","cc"); 
	    } 
	    catch (Exception ex) { 
	        ex.printStackTrace(); 
	    } 
	    try{  
	        String sql="select * from app_info";  
	        ps=conn.prepareStatement(sql);  
	        rs=ps.executeQuery();  
	        while(rs.next()){  
	    		AppInfo appInfo = new AppInfo();
	        	appInfo.setId(rs.getInt("id"));
	        	appInfo.setSwitch_ram_range(rs.getInt("range"));
	        	appInfo.setApp_name(rs.getString("name"));
	        	appInfo.setApp_id(rs.getString("size"));
	        	appInfo.setPackage_name(rs.getString("package_name"));
	        	appInfo.setAct(rs.getString("time"));
	        	appInfoList.add(appInfo);
	        }  
	          
	    }catch(Exception e){  
	        e.printStackTrace();  
	    }finally{  
	        try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e2) {
				// TODO: handle exception
			}  
	    }
		return appInfoList;  
	}
}


jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="pojo.AppInfo"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0034)http://www.dadagm.com/fileslist.do -->
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>cc应用列表</title>
		<link href="./css/admin.css" rel="stylesheet" type="text/css"/>
		<link href="./css/tab_menu.css" rel="stylesheet" type="text/css"/>
	</head>
	
	
	<body>
	<div id="admin-box">
		<div class="admin-listname"> </div>
        <table width="960" border="0" align="center" cellpadding="0" cellspacing="1">
          <tbody>
	          <tr class="admin-list">
	            <td width="100">id</td>
	            <td width="20">range</td>
	            <td width="150">name</td>
	            <td width="250">size</td>
	            <td width="150">package_name</td>
	            <td width="200">time</td>
	          </tr>
	          <%
		          ArrayList<AppInfo> appInfoList = 
					(ArrayList<AppInfo>)request.getAttribute("appInfoList");
		          for(int i=0; i<appInfoList.size(); i++){
		          		AppInfo aInfo = appInfoList.get(i);%>
		          		<tr class="admin-content">
					    <td style="text-align:center;"><%=aInfo.getId()%></td>
			            <td style="text-align:center;"><%=aInfo.getRange()%></td>
			            <td style="text-align:center;"><%=aInfo.getName()%></td>
			            <td style="text-align:center;"><%=aInfo.getSize()%></td>
			            <td style="text-align:center;"><%=aInfo.getPackage_name()%></td>
						<td style="text-align:center;"><%=aInfo.getTime()%></td>
			          </tr>
		       <% }%>
        	</tbody>
		</table>
		<div class="modify-link">   
            <a href="show_app_info.jsp">修改</a>
		</div>
	</div>  
	</body>
</html>

你可能感兴趣的:(javabean)