整体思路
由JSP实现页面显示部分,Servlet主要用于处理客户端传来的Http请求,并返回一个响应,MySQL数据库负责存储表单生成所需要的数据,而JDBC负责与数据库建立联系。
首先在数据库中建立好信息表单,作为备调用的后台数据。
工程结构图示
根据数据表单,建立Profit类,实现setter(),getter()方法
Profit.java
package beans;
public class Profit {
private String name;
private int id;
private int costPrice;
private int sellPrice;
private String manufacturer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCostPrice() {
return costPrice;
}
public void setCostPrice(int costPrice) {
this.costPrice = costPrice;
}
public int getSellPrice() {
return sellPrice;
}
public void setSellPrice(int sellPrice) {
this.sellPrice = sellPrice;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}
建立与数据库的连接
Jdbc.java
package jdbc;
import java.sql.*;
public class Jdbc {
// url固定格式 ,3360为MySQL默认端口号,god为数据库名称
private static String url = "jdbc:mysql://127.0.0.1:3306/god";
private static String username = "root";
private static String password = "www9183com";
public static Connection conn;
public static Connection getConnection() {
try {
// 反射,加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
/*
* 驱动程序管理器DriverManager,此类负责管理JDBC驱动程序的基本服务,
* 是JDBC的管理层,作用于用户和驱动程序之间,负责跟踪可用的驱动程序,并在数据库 和驱动程序之间建立连接。
* 获取Connection对象需要使用DriverManager对象,该对象的getConnection()方法
* 通过数据库连接url、数据库用户名和数据库密码创建Connection对象
*/
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
获取数据库中的数据,并将其存放到ArrayList实例中
Service.java
package service;
import java.awt.List;
import java.sql.*;
import java.util.*;
import beans.Profit;
import jdbc.Jdbc;
public class Service {
/*
* Connection接口用于创建数据库的连接(会话),只有获得该连接对象后才能访问数据库,
* 并操作数据表。
* Statement接口用于执行静态SQL语句,并返回一个生成结果的对象,
* 该接口的对象通过Connection实例的createStatement()方法获得。
* 利用该对象把静态SQL语句发送到数据库编译执行,然后返回数据库的处理结果。
* ResultSet接口用于保存JDBC执行查询时返回的结果集,该结果集与数据库表字段
* 相对应。即由行和列组成,并且在ResultSet结果集的行上提供指针。最初指针指向结果集
* 的第一行之前,调用next()方法可将指针移至下一行,如果下一行没有数据,则返回false。
*/
private Connection dbconn;
private Statement st;
private ResultSet rs;
private String sql;
private ArrayList list;
private Profit pf;
public ArrayList getProfit(){
list =new ArrayList();
//使用自定义的Jdbc类下的getConnection()方法取得与数据库的连接
dbconn=Jdbc.getConnection();
try {
st=dbconn.createStatement();
sql="select * from goods";
rs=st.executeQuery(sql);
while(rs.next()){
pf=new Profit();
pf.setName(rs.getString("name"));
pf.setId(rs.getInt("id"));
pf.setCostPrice(rs.getInt("costPrice"));
pf.setSellPrice(rs.getInt("sellPrice"));
pf.setManufacturer(rs.getString("manufacturer"));
list.add(pf);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
Servlet接收到数据传输请求,将ArrayList的实例list传输给页面来显示
ShowReport.java
package servlet;
import java.awt.List;
import java.io.IOException;
import java.util.ArrayList;
import service.Service;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//通常所说的Servlet类就是指HttpSerlet类,在使用Servlet时可以直接继承HttpServlet。
public class ShowReport extends HttpServlet {
public ShowReport() {
super();
}
public void init() throws ServletException {
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Service service=new Service();
ArrayList list=service.getProfit();
/*
* getSession()方法返回与客户端相关联的HttpSession对象
* HttpSession提供一种方式,跨多个页面请求或对 Web 站点的多次访问标识用户并存储有关该用户的信息。
* setAttribute(string name,Object value)是类HttpSession下的方法,将对象value赋予名称name
* 绑定到此会话中。
*/
req.getSession().setAttribute("phone", list);
//请求重定向
resp.sendRedirect("index.jsp");
}
}
页面显示部分,表单生成
index.jsp
<%@ page language="java" import="java.util.*,beans.*"
contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Java环境生成报表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
table.hovertable {
font-family: 黑体;
font-size: 13px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertable th {
background-color: #c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertable tr {
background-color: #d4e3e5;
}
table.hovertable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
</style>
</head>
<body>
<form action="ShowReport" method="post">
<input type="submit" value="生成表单">
</form>
<table class="hovertable">
<tr>
<th colspan="5">手机信息表</th>
</tr>
<tr>
<th>序号</th>
<th>品牌</th>
<th>成本价</th>
<th>销售价</th>
<th>厂商</th>
</tr>
<%
ArrayList list = null;
//session为JSP内置对象
if (session.getAttribute("phone") != null) {
list = (ArrayList) session.getAttribute("phone");
if (list.size() > 0) {
Profit pf;
for (int i = 0; i < list.size(); i++) {
pf = new Profit();
pf = (Profit) list.get(i);
%>
<tr οnmοuseοver="this.style.backgroundColor='#ffff66'"
οnmοuseοut="this.style.backgroundColor='#d4e3e5'">
<td><%=pf.getId()%></td>
<td><%=pf.getName()%></td>
<td><%=pf.getCostPrice()%></td>
<td><%=pf.getSellPrice()%></td>
<td><%=pf.getManufacturer()%></td>
</tr>
<%
}
}
}
%>
</table>
</body>
</html>
效果图