什么是报表:
1、以格式化的形式输出数据2、对数据进行分组、汇总、计算等操作。
3、通过报表、图表或嵌入图片图像等形式来丰富数据的显示。
报表生成的关键要素:
1、后台数据抽取;2、数据项逻辑运算;
3、前台表格展现;
报表在项目中的地位:
1、面向管理层和决策层;2、充分展现系统数据价值。
报表分类:
普通的二维报表普通的图表
嵌入式的组合报表
代码
entity.Good.java
private int goodsid;
private String goodsName;
private int costPrice;
private int sellingPrice;
private String manufacturer;
entity.Profit.java
private int goodsid;
private String goodsName;
private int costPrice;
private int sellingPrice;
private int tradingNum;
private int times;
private int profit;
util.DBUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String url="jdbc:mysql://localhost:3306/report?useUnicode=true&characterEncoding=UTF-8";
private static final String user="root";
private static final String password="123456";
static Connection conn=null;
static
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection()
{
return conn;
}
}
dao.ProfitDao.java
package dao;
import util.DBUtil;
import entity.Profit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProfitDao {
public List getProfit()
{
Connection conn=null;
PreparedStatement stmt = null;
PreparedStatement stmt2 = null;
ResultSet rs = null;
ResultSet rs2 = null;
String sql, sql2;
List list=new ArrayList();
try {
conn=DBUtil.getConnection();
sql="select GOODS_ID goodsid,GOODS_NAME goodsName,COST_PRICE costPrice,SELLING_PRICE sellingPrice,TRADING_NUMBER tradingNum " +
"from goods , trading where GOODS_ID=TRADING_GOODS_ID;";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
while(rs.next())
{
Profit pf=new Profit();
pf.setCostPrice(rs.getInt("costPrice"));
pf.setGoodsid(rs.getInt("goodsid"));
pf.setGoodsName(rs.getString("goodsName"));
pf.setSellingPrice(rs.getInt("sellingPrice"));
pf.setTradingNum(rs.getInt("tradingNum"));
int temp=pf.getSellingPrice()-pf.getCostPrice();
// sql2="select SUM(TRADING_ID) tradingID from trading where TRADING_GOODS_ID="+pf.getGoodsid();
// stmt2=conn.prepareStatement(sql2);
// rs2=stmt.executeQuery();
// pf.setProfit(rs2.getInt("tradingID")*temp);
//
// sql2="select count(TRADING_ID) tradingCount from trading where TRADING_GOODS_ID="+pf.getGoodsid()+";";
// stmt2=conn.prepareStatement(sql2);
// rs2=stmt.executeQuery();
// pf.setTimes(rs2.getInt("tradingCount"));
list.add(pf);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
rs=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
stmt=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
}
servlet.ReportServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw=response.getWriter();
pw.write("123");
ProfitDao dao=new ProfitDao();
List list=dao.getProfit();
request.getSession().setAttribute("ProfitList", list);
response.sendRedirect("../index.jsp");
}
index.jsp
<body>
<form action="servlet/ReportServlet" method="post" >
<input type="submit" value="生成报表"/>
</form>
<table>
<tr colspan="5">利润表</tr>
<th>序号</th>
<th>商品名称</th>
<th>卖出数量</th>
<th>交易笔数</th>
<th>盈利额</th>
</table>
<%
List list =new ArrayList();
if(session.getAttribute("ProfitList")!=null)
{
list=(List)session.getAttribute("ProfitList");
if(list.size()>0)
{
for(int i=0;i<list.size();i++)
{
Profit pf=(Profit)list.get(i);
%>
<tr>
<td>
<%= pf.getCostPrice()%>
</td>
<td>
<%= pf.getGoodsid()%>
</td>
</tr>
<%
}
}
}
%>
</body>