理财产品信息管理系统项目代码分享
程序员文章站
2022-07-14 22:32:47
...
在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<Product> queryAllProductInformation() {
List<Product> pros=new ArrayList<Product>();
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<Product> queryProduct(String id, String risk) {
List<Product> pros=new ArrayList<Product>();
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<Product> queryProductById(String id) {
List<Product> pros=new ArrayList<Product>();
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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
*{
padding: 0px;
margin: 0px;
}
.f_l{
float: left;
}
.f_r{
float: right;
}
.c{
clear: both;
}
#container{
width:600px;
height:auto;
padding:20px;
margin: 0px auto;
border: solid 1px black;
}
#d1{
margin-top:30px;
padding:0px;
height:60px;
}
table{
width:600px;
text-align: center;
}
li{
list-style: none;
padding:0px;
}
table tr:nth-child(odd){
background: #ccc;
}
</style>
<script type="text/javascript">
function check(){
var risk=document.getElementById("risk").value;
if(risk!="R0"){
return true;
}else{
return false;
}
}
</script>
</head>
<body>
<div id="container">
<form method="post" action="QueryProductServlet" onsubmit="return checked">
<div id="d1">
<ul>
<li class="f_l">产品代码:<input type="text" id="id" name="id"/></li>
<li class="f_l">
风险评估<select name="risk" id="risk">
<option value="R0" selected="selected">--请选择--</option>
<option value="R1">R1</option>
<option value="R2">R2</option>
<option value="R3">R3</option>
</select>
</li>
<li class="f_l"><input type="submit" value=" 查询 "/></li>
<li class="f_r"><a href="addProduct.jsp">新增理财信息</a></li>
</ul>
</div>
<div class="c" id="d2">
<table border="1">
<tr>
<th>产品代码</th>
<th>风险评级</th>
<th>预期收益</th>
<th>发售起始日</th>
<th>发售截止日</th>
<th>产品到期日</th>
</tr>
<c:forEach var="p" items="${requestScope.pros}">
<tr>
<td>${p.id}</td>
<td>${p.risk}</td>
<td>${p.income}</td>
<td>${p.saleStarting}</td>
<td>${p.saleEnd}</td>
<td>${p.end}</td>
</tr>
</c:forEach>
</table>
</div>
</form>
</div>
</body>
</html>
列出所有产品的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<Product> 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<Product> 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<Product> pros=productDao.queryProductById(id);
request.setAttribute("pros", pros);
request.getRequestDispatcher("homePage.jsp").forward(request, response);
}else {
ProductDAO productDao=new ProductDAO();
List<Product> 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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加理财信息页面</title>
<style type="text/css">
*{
padding: 0px;
margin: 0px;
}
#container{
width: 365px;
height: 260px;
margin: 0px auto;
padding:40px;
border: solid 1px black;
background-color: #f3f3f3;
}
h1{
height: 60px;
color:blue;
text-align: center;
}
p{
line-height: 50px;
text-align: center;
}
#d2{
font-size: 10px;
}
</style>
<script type="text/javascript">
function isId1(){
var id=document.getElementById("id").value;
if(id!=null){
return true;
}else{
return false;
}
}
function checkRisk(){
var risk=document.getElementById("risk").value;
if(risk!=null){
return true
}else{
return false;
}
}
function isIncome1(){
var income=document.getElementById("income").value;
if(income!=null){
return true;
}else{
return false;
}
}
function checkSaleStarting1(){
var saleStarting=document.getElementById("saleStarting").value;
var regDate=/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/;
if(regDate.test(saleStarting)&&saleStarting!=null){
return true;
}else{
alert("发售起始日期格式输入不正确!");
return false;
}
}
function checkSaleEnd1(){
var saleEnd=document.getElementById("saleEnd").value;
var regDate=/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/;
if(regDate.test(saleEnd)&&saleEnd!=null){
return true;
}else{
alert("发售截止日期格式输入不正确!");
return false;
}
}
function checkEnd1(){
var end=document.getElementById("end").value;
var regDate=/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/;
if(regDate.test(end)&&end!=null){
return true;
}else{
alert("产品到期日期格式输入不正确!");
return false;
}
}
function goBack1(){
location.href="QueryProductServlet";
}
function check(){
var checkId1=isId1();
var checkIncome1=isIncome1();
var checkSaleStarting1=checkSaleStarting1();
var checkSaleEnd1=checkSaleEnd1();
var checkEnd1=checkEnd1();
var checkRisk=checkRisk()
if(checkId1&&checkRisk&&checkIncome1&&checkSaleStarting1&&checkSaleEnd1&&checkEnd1){
return true;
}else{
return false;
}
}
</script>
</head>
<body>
<div id="container">
<form method="post" action="AddProductServlet" onsubmit="return check()">
<h1>新增理财信息</h1>
<table>
<tr>
<td>产品代码:</td>
<td><input type="text" id="id" name="id" onblur="isId1()" /></td>
<td></td>
</tr>
<tr>
<td>风险评级:</td>
<td>
<select name="risk" id="risk" onchange="checkRisk()">
<option>--请选择--</option>
<option value="R1">R1</option>
<option value="R2">R2</option>
<option value="R3">R3</option>
</select>
</td>
<td></td>
</tr>
<tr>
<td>预期收益:</td>
<td><input type="text" id="income" name="income" onblur="isIncome1()" /></td>
<td></td>
</tr>
<tr>
<td>发售起始日:</td>
<td><input type="text" id="saleStarting" name="saleStarting" onblur="checkSaleStarting1()" /></td>
<td id="d2">yyyy-MM-dd格式</td>
</tr>
<tr>
<td>发售截止日:</td>
<td><input type="text" id="saleEnd" name="saleEnd" onblur="checkSaleEnd1()" /></td>
<td id="d2">yyyy-MM-dd格式</td>
</tr>
<tr>
<td>产品到期日:</td>
<td><input type="text" id="end" name="end" onblur="checkEnd1()" /></td>
<td id="d2">yyyy-MM-dd格式</td>
</tr>
</table>
<p>
<input type="submit" value=" 保存 "/>
<input type="reset" value=" 重置 "/>
<input type="button" value=" 返回 " onclick="goBack1()"/>
</p>
</form>
</div>
</body>
</html>
添加产品信息的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");
}
}
}
上一篇: es命令行用法