在MySql中创建数据库表
向数据库表中添加数据
在Eclipse中创建数据库表对应的实体类
package com.qf.entity;
public class Product {
private String id;
private String risk;
private String income;
private String saleStarting;
private String saleEnd;
private String end;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRisk() {
return risk;
}
public void setRisk(String risk) {
this.risk = risk;
}
public String getIncome() {
return income;
}
public void setIncome(String income) {
this.income = income;
}
public String getSaleStarting() {
return saleStarting;
}
public void setSaleStarting(String saleStarting) {
this.saleStarting = saleStarting;
}
public String getSaleEnd() {
return saleEnd;
}
public void setSaleEnd(String saleEnd) {
this.saleEnd = saleEnd;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
@Override
public String toString() {
return "Product [id=" + id + ", risk=" + risk + ", income=" + income + ", saleStarting=" + saleStarting
+ ", saleEnd=" + saleEnd + ", end=" + end + "]";
}
}
创建jdbc工具类
package com.qf.dao;
/**
* jdbc工具类
* @author Administrator
*
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
public static Connection conn=null;
public static PreparedStatement state=null;
public static ResultSet rs=null;
/**
* 获得连接对象的发方法
* @throws ClassNotFoundException
* @throws SQLException
*
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/lianxi", "root", "147258");
return conn;
}
/**
* jdbc增加修改删除的方法
* @param sql
* @param obs 动态参数
* @return
*/
public static int update(String sql,Object...obs) {
int result=0;
try {
DBUtils.getConnection();
state=conn.prepareStatement(sql);
for (int i = 0; i < obs.length; i++) {
state.setObject(i+1, obs[i]);
}
result=state.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* jdbc查询的方法
* @param sql
* @param obs 动态参数
* @return
*/
public static ResultSet query(String sql,Object...obs) {
try {
DBUtils.getConnection();
state=conn.prepareStatement(sql);
for (int i = 0; i < obs.length; i++) {
state.setObject(i+1, obs[i]);
}
rs=state.executeQuery();
}catch (Exception e) {
e.printStackTrace();
}
return rs;
}
/**
* 关闭jdbc对象的方法
*/
public static void closeObject() {
try {
if(rs!=null) {
rs.close();
}
if(state!=null) {
state.close();
}
if(conn!=null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
创建对数据库表进行操作的DAO类
package com.qf.dao;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.qf.entity.Product;
public class ProductDAO {
/**
* 查找数据库表中所有信息的方法
* @return
*/
public List queryAllProductInformation() {
List pros=new ArrayList();
try {
String sql="select Id,Risk,Income,SaleStarting,SaleEnd,End from FinancingProduct order by SaleStarting desc";
DBUtils.rs=DBUtils.query(sql);
while(DBUtils.rs.next()) {
Product p=new Product();
p.setId(DBUtils.rs.getString("Id"));
p.setRisk(DBUtils.rs.getString("Risk"));
p.setIncome(DBUtils.rs.getString("Income"));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
p.setSaleStarting(sdf.format(DBUtils.rs.getDate("SaleStarting")));
p.setSaleEnd(sdf.format(DBUtils.rs.getDate("SaleEnd")));
p.setEnd(sdf.format(DBUtils.rs.getDate("End")));
pros.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.closeObject();
}
return pros;
}
/**
* 根据输入的产品代码和风险评级查找理财信息
* @param id 用户输入的产品代码
* @param risk 用户选择的风险评级
* @return
*/
public List queryProduct(String id, String risk) {
List pros=new ArrayList();
try {
String sql="select Id,Risk,Income,SaleStarting,SaleEnd,End from FinancingProduct where Id like ? and Risk=? order by SaleStarting desc";
DBUtils.rs=DBUtils.query(sql,"%"+id+"%",risk);
while(DBUtils.rs.next()) {
Product p=new Product();
p.setId(DBUtils.rs.getString("Id"));
p.setRisk(DBUtils.rs.getString("Risk"));
p.setIncome(DBUtils.rs.getString("Income"));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
p.setSaleStarting(sdf.format(DBUtils.rs.getDate("SaleStarting")));
p.setSaleEnd(sdf.format(DBUtils.rs.getDate("SaleEnd")));
p.setEnd(sdf.format(DBUtils.rs.getDate("End")));
pros.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.closeObject();
}
return pros;
}
public List queryProductById(String id) {
List pros=new ArrayList();
try {
String sql="select Id,Risk,Income,SaleStarting,SaleEnd,End from FinancingProduct where Id like ? order by SaleStarting desc";
DBUtils.rs=DBUtils.query(sql,"%"+id+"%");
while(DBUtils.rs.next()) {
Product p=new Product();
p.setId(DBUtils.rs.getString("Id"));
p.setRisk(DBUtils.rs.getString("Risk"));
p.setIncome(DBUtils.rs.getString("Income"));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
p.setSaleStarting(sdf.format(DBUtils.rs.getDate("SaleStarting")));
p.setSaleEnd(sdf.format(DBUtils.rs.getDate("SaleEnd")));
p.setEnd(sdf.format(DBUtils.rs.getDate("End")));
pros.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.closeObject();
}
return pros;
}
public int qureyId(Product p) {
int result=0;
try {
String sql="select count(Id) from FinancingProduct where Id=?";
DBUtils.rs=DBUtils.query(sql,p.getId());
while(DBUtils.rs.next()) {
result=(DBUtils.rs.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.closeObject();
}
return result;
}
public int addProduct(Product p) {
String sql="insert into FinancingProduct(Id,Risk,Income,SaleStarting,SaleEnd,End) values(?,?,?,?,?,?)";
int result=DBUtils.update(sql,p.getId(),p.getRisk(),p.getIncome(),p.getSaleStarting(),p.getSaleEnd(),p.getEnd());
DBUtils.closeObject();
return result;
}
}
前端首页的jsp代码
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
列出所有产品的Servlet代码
package com.qf.servlet;
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.qf.dao.ProductDAO;
import com.qf.entity.Product;
/**
* Servlet implementation class QueryProductServlet
*/
@WebServlet("/QueryProductServlet")
public class QueryProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String id=request.getParameter("id");
String risk=request.getParameter("risk");
if(id==null && risk==null) {
ProductDAO productDao=new ProductDAO();
List pros=productDao.queryAllProductInformation();
request.setAttribute("pros", pros);
request.getRequestDispatcher("homePage.jsp").forward(request, response);
}else if (id.equals("") && "R0".equals(risk)){
ProductDAO productDao=new ProductDAO();
List pros=productDao.queryAllProductInformation();
request.setAttribute("pros", pros);
request.getRequestDispatcher("homePage.jsp").forward(request, response);
}else if(!id.equals("") && "R0".equals(risk)){
ProductDAO productDao=new ProductDAO();
List pros=productDao.queryProductById(id);
request.setAttribute("pros", pros);
request.getRequestDispatcher("homePage.jsp").forward(request, response);
}else {
ProductDAO productDao=new ProductDAO();
List pros=productDao.queryProduct(id,risk);
request.setAttribute("pros", pros);
request.getRequestDispatcher("homePage.jsp").forward(request, response);
}
}
}
前端添加理财产品信息的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
添加理财信息页面
添加产品信息的servlet代码
package com.qf.servlet;
import java.io.IOException;
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.qf.dao.ProductDAO;
import com.qf.entity.Product;
/**
* Servlet implementation class AddProductServlet
*/
@WebServlet("/AddProductServlet")
public class AddProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Product p=new Product();
p.setId(request.getParameter("id"));
p.setRisk(request.getParameter("risk"));
p.setIncome(request.getParameter("income"));
p.setSaleStarting(request.getParameter("saleStarting"));
p.setSaleEnd(request.getParameter("saleEnd"));
p.setEnd(request.getParameter("end"));
ProductDAO productDao=new ProductDAO();
int result=productDao.qureyId(p);
if(result==0) {
int result2=productDao.addProduct(p);
if(result2>0) {
response.getWriter().write("添加成功!");
response.sendRedirect("QueryProductServlet");
}else {
response.getWriter().write("添加失败!");
response.sendRedirect("addProduct.jsp");
}
}else {
response.getWriter().write("产品代码重复!");
response.sendRedirect("addProduct.jsp");
}
}
}