数据无法在表格中显现,数据库测试过了,没问题
package service; //处理数据 import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.eclipse.jdt.internal.compiler.batch.Main; import Jdbc.JdbcCon; import beans.Profit; public class Service { private static Connection con=null; private static PreparedStatement st=null,st1=null; private static ResultSet rs=null,rs1=null; private static String sql,sql1; private static List list;//储存商品信息集合 private static Profit pf;//利润 public static List getReport(){ try { sql="select goodsList.goods_Id goodsId,goodsList.goods_Name goodsName,"+ "goodsList.cost_Price costPrice,goodsList.sell_Price sellPrice from goodsList"; con=JdbcCon.getConnection(); st=con.prepareStatement(sql); rs=st.executeQuery(); list=new ArrayList(); int temp; while(rs.next()){ pf=new Profit(); pf.setGoods_Id(rs.getInt("goodsId")); System.out.println(rs.getInt("goodsId")); pf.setGoods_Name(rs.getString("goodsName")); System.out.println(rs.getString("goodsName")); pf.setCostPrice(rs.getInt("costPrice")); pf.setSellPrice(rs.getInt("sellPrice")); sql1="select sum(trading_Number) sumNumber from trading_Information where trading_Information.trading_goods_Id= "+rs.getInt("goodsId"); st1=con.prepareStatement(sql1); rs1=st1.executeQuery(); while(rs1.next()){ pf.setTrading_Number(rs1.getInt("sumNumber")); System.out.println(pf.getTrading_Number()); temp=rs.getInt("sellPrice")-rs.getInt("costPrice"); System.out.println(temp); pf.setPf(temp*rs1.getInt("sumNumber")); System.out.println(pf.getPf()); } list.add(pf); } return list; } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); return null; }finally{ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (st!=null) { try { st.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (rs1!=null) { try { rs1.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (st1!=null) { try { st1.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } }
package service;
import java.io.IOException;
import java.util.List;
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 com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
/**
* Servlet implementation class ShowReport
*/
@WebServlet("/ShowReport")
public class ShowReport extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowReport() {
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
doPost(request, response);
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List list;
list=Service.getReport();
request.getSession().setAttribute("PF",list);//通过session对象将后台信息传到前台
//response.sendRedirect("index2.jsp");
request.getRequestDispatcher("../index2.jsp").forward(request, response);
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="beans.Profit"%>
<%@page import="beans.Goods"%>
<%@page import="java.util.List"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>生成报表</title>
<style type="text/css">
.first{
color:#333333;
font-size:13px;
border-width:1px;
border-style:solid;
border-color:#999999;
border-collapse:collapse;
}
.first .second{
border-width:1px;
border-style:solid;
border-color:#a9c6c9;
background-color:#c3dde0;
padding:8px;
}
.three{
border:1px solid #a9c6c9;
padding:8px;
}
</style>
</head>
<body>
<form action="servlet/ShowReport" method="post">
<input type="button" value="生成报表">
<table class="first" border="1px" cellspacing="0">
<tr><th class="second" colspan="6">利润表</th></tr>
<tr onmousemove="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<th class="three">商品编号</th>
<th class="three">商品名称</th>
<th class="three">成本价</th>
<th class="three">售价</th>
<th class="three">交易数量</th>
<th class="three">利润</th>
</tr>
<%
//List list=(List)request.getSession().getAttribute("report");
int temp1=0,temp2=0;
List list=null;
if(session.getAttribute("PF")!=null){
list=(List)session.getAttribute("PF");
if(list.size()>0){
Profit pf;
for(int i=0;i<list.size();i++){
pf=new Profit();
pf=(Profit)list.get(i);
temp1+=pf.getTrading_Number();
temp2+=pf.getPf();
%>
<tr onmousemove="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<td class="three"><%=pf.getGoods_Id()%></td>
<td class="three"><%=pf.getGoods_Name() %></td>
<td class="three"><%=pf.getCostPrice() %></td>
<td class="three"><%=pf.getSellPrice()%></td>
<td class="three"><%=pf.getTrading_Number()%></td>
<td class="three"><%=pf.getPf()%></td>
</tr>
<%
}
}
}
%>
<tr onmousemove="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<th class="three" colspan="4">合计</th>
<td class="three"><%=temp1%></td>
<td class="three"><%=temp2%></td>
</tr>
</table>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>javaweb_5</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.javaweb4.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/servlet/ImageServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.javaweb4.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowReport</servlet-name>
<servlet-class>service.ShowReport</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowReport</servlet-name>
<url-pattern>/servlet/ShowReport</url-pattern>
</servlet-mapping>
</web-app>