JSP+Servlet+JDBC+MySQL实现表单生成
来源:互联网 发布:淘宝买家要求线下送货 编辑:程序博客网 时间:2024/04/29 05:09
整体思路
由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 onmouseover="this.style.backgroundColor='#ffff66'"onmouseout="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>
效果图
0 0
- JSP+Servlet+JDBC+MySQL实现表单生成
- 实践---原生jsp+servlet+jdbc+mysql实现分页功能
- jsp+servlet+jdbc 实现crud
- 分页详解jsp+servlet+jdbc+mysql
- BBS论坛留言板servlet+jsp+jdbc+mysql
- jdbc+servlet+jsp+mysql简单的例子
- JSP+Servlet+JDBC实现数据分页
- servlet+jsp+jdbc实现简单的登录功能(所用平台:win7+Eclipse+tomcat+mysql)
- 纯JSP+JDBC+MySQL生成登录注册
- Exam2 (JSP, Servlet, JDBC)
- JDBC-Servlet-JSP入门
- Jsp+Servlet+JDBC 登录
- 【J2EE】JDBC+JSP+Servlet
- jsp+servlet 表单验证
- Servlet+Jsp+Tomcat+Jstl+El+Jdbc+mysql+C3p0
- JSP Servlet JDBC MYSQL数据库中文字符显示为乱码
- 使用servlet,jdbc将mysql中数据显示在jsp页面中,且实现直接删除数据库数据
- 使用servlet,jdbc将mysql中数据显示在jsp页面中,且实现直接删除数据库数据
- Spring和springmvc父子容器关系
- 【C#基础】装箱与拆箱
- componentsJoinedByString和componentsSeparatedByString的使用
- python 条件判断与循环
- iOS开发实践之XML
- JSP+Servlet+JDBC+MySQL实现表单生成
- iOS实现空闲超时自动登出
- Java设计模式-策略模式(Strategy Pattern)
- Amazon的网络服务aws
- python flask介绍
- 新浪微博个人详情页
- MySQL的典型八小时问题
- 荷兰风车标志
- 10 绘制数字