写一个有消费报表和收入报表中做查询和添加的财务管理系统
时间: 2024-03-13 10:45:45 浏览: 19
以下是一个基于Java和MySQL数据库的简单财务管理系统,包括消费报表和收入报表的查询和添加功能:
消费报表:
1. 查询功能
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class ExpenseReport {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase"; // 数据库连接字符串
String user = "root"; // 数据库用户名
String password = "123456"; // 数据库密码
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建SQL语句执行对象
pstmt = conn.prepareStatement("SELECT * FROM expenses WHERE date >= ? AND date <= ?");
// 读取输入的查询日期范围
Scanner scanner = new Scanner(System.in);
System.out.print("请输入查询的开始日期(格式为yyyy-MM-dd):");
String startDateStr = scanner.nextLine();
System.out.print("请输入查询的结束日期(格式为yyyy-MM-dd):");
String endDateStr = scanner.nextLine();
// 将字符串转换为日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = dateFormat.parse(startDateStr);
Date endDate = dateFormat.parse(endDateStr);
// 设置SQL语句中的参数
pstmt.setDate(1, new java.sql.Date(startDate.getTime()));
pstmt.setDate(2, new java.sql.Date(endDate.getTime()));
// 执行SQL查询语句
rs = pstmt.executeQuery();
// 处理查询结果
while (rs.next()) {
int id = rs.getInt("id");
String category = rs.getString("category");
double amount = rs.getDouble("amount");
Date date = rs.getDate("date");
System.out.println("id=" + id + ", category=" + category + ", amount=" + amount + ", date=" + date);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接和SQL执行对象
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
在这个示例代码中,我们使用了Java的JDBC API来连接MySQL数据库,并利用PreparedStatement对象执行了一个根据日期范围查询消费报表的SQL语句。需要注意的是,我们在代码中使用了try-catch语句块来处理可能出现的异常,并在finally块中关闭了数据库连接和SQL执行对象,以防止资源泄漏。
2. 添加功能
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class ExpenseReport {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase"; // 数据库连接字符串
String user = "root"; // 数据库用户名
String password = "123456"; // 数据库密码
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建SQL语句执行对象
pstmt = conn.prepareStatement("INSERT INTO expenses(category, amount, date) VALUES (?, ?, ?)");
// 读取输入的消费信息
Scanner scanner = new Scanner(System.in);
System.out.print("请输入消费类别:");
String category = scanner.nextLine();
System.out.print("请输入消费金额:");
double amount = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
System.out.print("请输入消费日期(格式为yyyy-MM-dd):");
String dateStr = scanner.nextLine();
// 将字符串转换为日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateStr);
// 设置SQL语句中的参数
pstmt.setString(1, category);
pstmt.setDouble(2, amount);
pstmt.setDate(3, new java.sql.Date(date.getTime()));
// 执行SQL更新语句
int rowCount = pstmt.executeUpdate();
// 显示更新结果
if (rowCount > 0) {
System.out.println("成功添加" + rowCount + "条消费记录。");
} else {
System.out.println("添加消费记录失败。");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接和SQL执行对象
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
在这个示例代码中,我们使用了Java的JDBC API来连接MySQL数据库,并利用PreparedStatement对象执行了一个插入消费记录的SQL语句。需要注意的是,我们在代码中使用了try-catch语句块来处理可能出现的异常,并在finally块中关闭了数据库连接和SQL执行对象,以防止资源泄漏。
收入报表:
收入报表的查询和添加功能与消费报表类似,只需要将相关的表名、列名、SQL语句等做相应修改即可。以下是收入报表的示例代码:
查询功能:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class IncomeReport {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase"; // 数据库连接字符串
String user = "root"; // 数据库用户名
String password = "123456"; // 数据库密码
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建SQL语句执行对象
pstmt = conn.prepareStatement("SELECT * FROM incomes WHERE date >= ? AND date <= ?");
// 读取输入的查询日期范围
Scanner scanner = new Scanner(System.in);
System.out.print("请输入查询的开始日期(格式为yyyy-MM-dd):");
String startDateStr = scanner.nextLine();
System.out.print("请输入查询的结束日期(格式为yyyy-MM-dd):");
String endDateStr = scanner.nextLine();
// 将字符串转换为日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = dateFormat.parse(startDateStr);
Date endDate = dateFormat.parse(endDateStr);
// 设置SQL语句中的参数
pstmt.setDate(1, new java.sql.Date(startDate.getTime()));
pstmt.setDate(2, new java.sql.Date(endDate.getTime()));
// 执行SQL查询语句
rs = pstmt.executeQuery();
// 处理查询结果
while (rs.next()) {
int id = rs.getInt("id");
String category = rs.getString("category");
double amount = rs.getDouble("amount");
Date date = rs.getDate("date");
System.out.println("id=" + id + ", category=" + category + ", amount=" + amount + ", date=" + date);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接和SQL执行对象
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
添加功能:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class IncomeReport {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase"; // 数据库连接字符串
String user = "root"; // 数据库用户名
String password = "123456"; // 数据库密码
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
conn = DriverManager.getConnection(url, user, password);
// 创建SQL语句执行对象
pstmt = conn.prepareStatement("INSERT INTO incomes(category, amount, date) VALUES (?, ?, ?)");
// 读取输入的收入信息
Scanner scanner = new Scanner(System.in);
System.out.print("请输入收入类别:");
String category = scanner.nextLine();
System.out.print("请输入收入金额:");
double amount = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
System.out.print("请输入收入日期(格式为yyyy-MM-dd):");
String dateStr = scanner.nextLine();
// 将字符串转换为日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateStr);
// 设置SQL语句中的参数
pstmt.setString(1, category);
pstmt.setDouble(2, amount);
pstmt.setDate(3, new java.sql.Date(date.getTime()));
// 执行SQL更新语句
int rowCount = pstmt.executeUpdate();
// 显示更新结果
if (rowCount > 0) {
System.out.println("成功添加" + rowCount + "条收入记录。");
} else {
System.out.println("添加收入记录失败。");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接和SQL执行对象
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
以上是一个简单的财务管理系统示例,仅供参考。实际应用中,还需要加入更多的功能和安全性考虑,比如用户身份验证、数据校验、数据备份等等。