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

参考文章
[url="http://jzgl-javaeye.iteye.com/blog/647946"]第一个JSP+Servlet+JavaBean+JDBC示例程序[/url]

[url="http://www.cnblogs.com/dodohua/archive/2011/01/13/1934183.html"]jsp学习日记(一)javabean的使用和JavaBean+servlet的[/url]

[url="http://zhidao.baidu.com/question/199084061.html"]JSP+JavaBean+Servlet+MSSQLServer2000实现分页查询[/url]


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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值