jsp 语言 金融平台用户理财产品到期自动续投实战

JSP阿木 发布于 2025-07-03 11 次阅读


金融平台用户理财产品到期自动续投实战:JSP技术实现

随着金融科技的不断发展,理财产品在金融市场中扮演着越来越重要的角色。为了提高用户体验,许多金融平台都推出了理财产品到期自动续投的功能。本文将围绕这一主题,使用JSP技术实现一个简单的理财产品到期自动续投系统。

系统需求分析

在实现理财产品到期自动续投功能之前,我们需要明确以下需求:

1. 用户可以查看自己的理财产品列表。

2. 系统可以自动检测理财产品是否到期。

3. 到期后,系统可以自动续投用户选择的理财产品。

4. 用户可以查看自己的续投记录。

技术选型

为了实现上述需求,我们选择以下技术栈:

- 后端:Java + JSP

- 数据库:MySQL

- 前端:HTML + CSS + JavaScript

系统设计

数据库设计

我们需要设计数据库表结构。以下是几个关键表的设计:

1. `users`:存储用户信息。

- `id`:主键,用户ID。

- `username`:用户名。

- `password`:密码。

2. `products`:存储理财产品信息。

- `id`:主键,理财产品ID。

- `name`:理财产品名称。

- `interest_rate`:年化收益率。

- `min_investment`:最低投资金额。

- `max_investment`:最高投资金额。

3. `user_products`:存储用户理财产品信息。

- `id`:主键,用户理财产品ID。

- `user_id`:外键,用户ID。

- `product_id`:外键,理财产品ID。

- `amount`:投资金额。

- `start_date`:投资开始日期。

- `end_date`:投资到期日期。

4. `renewals`:存储用户续投记录。

- `id`:主键,续投记录ID。

- `user_id`:外键,用户ID。

- `product_id`:外键,理财产品ID。

- `amount`:续投金额。

- `renew_date`:续投日期。

JSP页面设计

接下来,我们需要设计JSP页面。以下是几个关键页面的设计:

1. `login.jsp`:用户登录页面。

2. `index.jsp`:用户理财产品列表页面。

3. `renew.jsp`:理财产品续投页面。

4. `renew_record.jsp`:用户续投记录页面。

代码实现

数据库连接

我们需要创建一个数据库连接类,用于连接MySQL数据库。

java

public class DBConnection {


private static final String URL = "jdbc:mysql://localhost:3306/financial_platform";


private static final String USER = "root";


private static final String PASSWORD = "password";

public static Connection getConnection() throws SQLException {


return DriverManager.getConnection(URL, USER, PASSWORD);


}


}


用户登录

在`login.jsp`页面,用户输入用户名和密码,提交表单后,后端Java代码验证用户信息。

java

public class LoginServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String username = request.getParameter("username");


String password = request.getParameter("password");

Connection conn = null;


PreparedStatement ps = null;


ResultSet rs = null;

try {


conn = DBConnection.getConnection();


String sql = "SELECT FROM users WHERE username=? AND password=?";


ps = conn.prepareStatement(sql);


ps.setString(1, username);


ps.setString(2, password);


rs = ps.executeQuery();

if (rs.next()) {


// 登录成功,跳转到理财产品列表页面


response.sendRedirect("index.jsp");


} else {


// 登录失败,跳转到登录页面


request.setAttribute("error", "用户名或密码错误!");


request.getRequestDispatcher("login.jsp").forward(request, response);


}


} catch (SQLException e) {


e.printStackTrace();


} finally {


// 关闭资源


try {


if (rs != null) rs.close();


if (ps != null) ps.close();


if (conn != null) conn.close();


} catch (SQLException e) {


e.printStackTrace();


}


}


}


}


理财产品列表

在`index.jsp`页面,用户可以查看自己的理财产品列表。后端Java代码从数据库中查询用户理财产品信息。

java

public class ProductListServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String userId = (String) request.getSession().getAttribute("userId");

Connection conn = null;


PreparedStatement ps = null;


ResultSet rs = null;

try {


conn = DBConnection.getConnection();


String sql = "SELECT FROM user_products WHERE user_id=?";


ps = conn.prepareStatement(sql);


ps.setString(1, userId);


rs = ps.executeQuery();

List<UserProduct> userProducts = new ArrayList<>();


while (rs.next()) {


UserProduct userProduct = new UserProduct();


userProduct.setId(rs.getInt("id"));


userProduct.setUserId(rs.getInt("user_id"));


userProduct.setProductId(rs.getInt("product_id"));


userProduct.setAmount(rs.getDouble("amount"));


userProduct.setStartDate(rs.getDate("start_date"));


userProduct.setEndDate(rs.getDate("end_date"));


userProducts.add(userProduct);


}

request.setAttribute("userProducts", userProducts);


request.getRequestDispatcher("index.jsp").forward(request, response);


} catch (SQLException e) {


e.printStackTrace();


} finally {


// 关闭资源


try {


if (rs != null) rs.close();


if (ps != null) ps.close();


if (conn != null) conn.close();


} catch (SQLException e) {


e.printStackTrace();


}


}


}


}


理财产品续投

在`renew.jsp`页面,用户可以选择要续投的理财产品,并输入续投金额。后端Java代码处理续投请求。

java

public class RenewServlet extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String userId = (String) request.getSession().getAttribute("userId");


int productId = Integer.parseInt(request.getParameter("productId"));


double amount = Double.parseDouble(request.getParameter("amount"));

Connection conn = null;


PreparedStatement ps = null;


ResultSet rs = null;

try {


conn = DBConnection.getConnection();


// 检查理财产品是否到期


String checkSql = "SELECT FROM user_products WHERE user_id=? AND product_id=? AND end_date<=CURDATE()";


ps = conn.prepareStatement(checkSql);


ps.setInt(1, userId);


ps.setInt(2, productId);


rs = ps.executeQuery();

if (rs.next()) {


// 续投理财产品


String renewSql = "INSERT INTO renewals (user_id, product_id, amount, renew_date) VALUES (?, ?, ?, CURDATE())";


ps = conn.prepareStatement(renewSql);


ps.setInt(1, userId);


ps.setInt(2, productId);


ps.setDouble(3, amount);


ps.executeUpdate();

// 更新用户理财产品信息


String updateSql = "UPDATE user_products SET amount=amount+? WHERE user_id=? AND product_id=?";


ps = conn.prepareStatement(updateSql);


ps.setDouble(1, amount);


ps.setInt(2, userId);


ps.setInt(3, productId);


ps.executeUpdate();

// 跳转到续投记录页面


response.sendRedirect("renew_record.jsp");


} else {


// 理财产品未到期,跳转到续投页面


request.setAttribute("error", "理财产品未到期,无法续投!");


request.getRequestDispatcher("renew.jsp").forward(request, response);


}


} catch (SQLException e) {


e.printStackTrace();


} finally {


// 关闭资源


try {


if (rs != null) rs.close();


if (ps != null) ps.close();


if (conn != null) conn.close();


} catch (SQLException e) {


e.printStackTrace();


}


}


}


}


续投记录

在`renew_record.jsp`页面,用户可以查看自己的续投记录。

java

public class RenewRecordServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String userId = (String) request.getSession().getAttribute("userId");

Connection conn = null;


PreparedStatement ps = null;


ResultSet rs = null;

try {


conn = DBConnection.getConnection();


String sql = "SELECT FROM renewals WHERE user_id=?";


ps = conn.prepareStatement(sql);


ps.setString(1, userId);


rs = ps.executeQuery();

List<Renewal> renewals = new ArrayList<>();


while (rs.next()) {


Renewal renewal = new Renewal();


renewal.setId(rs.getInt("id"));


renewal.setUserId(rs.getInt("user_id"));


renewal.setProductId(rs.getInt("product_id"));


renewal.setAmount(rs.getDouble("amount"));


renewal.setRenewDate(rs.getDate("renew_date"));


renewals.add(renewal);


}

request.setAttribute("renewals", renewals);


request.getRequestDispatcher("renew_record.jsp").forward(request, response);


} catch (SQLException e) {


e.printStackTrace();


} finally {


// 关闭资源


try {


if (rs != null) rs.close();


if (ps != null) ps.close();


if (conn != null) conn.close();


} catch (SQLException e) {


e.printStackTrace();


}


}


}


}


总结

本文使用JSP技术实现了一个简单的理财产品到期自动续投系统。通过数据库设计、JSP页面设计和Java代码实现,我们成功实现了用户登录、理财产品列表、理财产品续投和续投记录等功能。在实际应用中,可以根据需求进行功能扩展和优化。