From 047661929f7f57de87d11da8ed149ac2c8f451e2 Mon Sep 17 00:00:00 2001
From: 86191 <2160251938@qq.com>
Date: Fri, 26 Jul 2024 09:06:13 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 38 +++++++++
.idea/.gitignore | 8 ++
.idea/encodings.xml | 8 ++
.idea/misc.xml | 15 ++++
pom.xml | 42 ++++++++++
.../java/org/example/BillApplication.java | 68 ++++++++++++++++
src/main/java/org/example/dao/BillDao.java | 77 +++++++++++++++++++
src/main/java/org/example/domain/Bill.java | 28 +++++++
8 files changed, 284 insertions(+)
create mode 100644 .gitignore
create mode 100644 .idea/.gitignore
create mode 100644 .idea/encodings.xml
create mode 100644 .idea/misc.xml
create mode 100644 pom.xml
create mode 100644 src/main/java/org/example/BillApplication.java
create mode 100644 src/main/java/org/example/dao/BillDao.java
create mode 100644 src/main/java/org/example/domain/Bill.java
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ff6309
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..35410ca
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..63574ec
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..13cdae7
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..83d22c0
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,42 @@
+
+
+ 4.0.0
+
+ org.example
+ billManager
+ 1.0-SNAPSHOT
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+ mysql
+ mysql-connector-java
+ 8.0.26
+
+
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.22
+
+
+
+
+
diff --git a/src/main/java/org/example/BillApplication.java b/src/main/java/org/example/BillApplication.java
new file mode 100644
index 0000000..5107a95
--- /dev/null
+++ b/src/main/java/org/example/BillApplication.java
@@ -0,0 +1,68 @@
+package org.example;
+
+import org.example.dao.BillDao;
+import org.example.domain.Bill;
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Scanner;
+
+public class BillApplication {
+ public static void main(String[] args) {
+
+ // 设置编码(仅在需要时启用)
+ try {
+ System.setOut(new java.io.PrintStream(System.out,true,"UTF-8"));
+ } catch (java.io.UnsupportedEncodingException e) {
+ System.err.println("无法设置UTF-8编码:" + e.getMessage());
+ }
+
+ // 数据库连接配置
+ String jdbcURL = "jdbc:mysql://localhost:3306/test";
+ String jdbcUsername = "mengyu";
+ String jdbcPassword = "@Liu8023520";
+
+ BillDao billDao = new BillDao(jdbcURL,jdbcUsername,jdbcPassword);
+
+ Scanner scanner = new Scanner(System.in);
+
+ while (true) {
+ System.out.println("请选择操作:1.添加账单 2.查询账单 3.退出");
+ int choice = scanner.nextInt();
+ // 消耗换行符
+ scanner.nextLine();
+
+ if (choice == 1) {
+ System.out.println("输入账单日期:");
+ String date = scanner.nextLine();
+ System.out.println("输入账单金额:");
+ double amount = scanner.nextDouble();
+ // 消耗换行符
+ scanner.nextLine();
+
+ Bill bill = new Bill(0,date,amount);
+ try {
+ billDao.add(bill);
+ System.out.println("账单添加成功!");
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ } else if (choice == 2) {
+ try {
+ List bills = billDao.selectAll();
+ for (Bill bill :bills) {
+ System.out.println(bill);
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ } else if (choice == 3) {
+ break;
+ } else {
+ System.out.println("无效选择,请重新输入!");
+ }
+ }
+
+ scanner.close();
+ }
+}
diff --git a/src/main/java/org/example/dao/BillDao.java b/src/main/java/org/example/dao/BillDao.java
new file mode 100644
index 0000000..6f1b3e7
--- /dev/null
+++ b/src/main/java/org/example/dao/BillDao.java
@@ -0,0 +1,77 @@
+package org.example.dao;
+
+import org.example.domain.Bill;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class BillDao {
+ // 数据库连接地址
+ private String jdbcURL;
+ // 数据库用户名
+ private String jdbcUsername;
+ // 数据库密码
+ private String jdbcPassword;
+ // 数据库连接
+ private Connection jdbcConnection;
+
+ // 构造函数
+ public BillDao(String jdbcURL, String jdbcUsername, String jdbcPassword) {
+ this.jdbcURL = jdbcURL;
+ this.jdbcUsername = jdbcUsername;
+ this.jdbcPassword = jdbcPassword;
+ }
+
+ // 连接数据库
+ protected void connect() throws SQLException {
+ if (jdbcConnection == null || jdbcConnection.isClosed()) {
+ jdbcConnection = DriverManager.getConnection(jdbcURL,jdbcUsername,jdbcPassword);
+ }
+ }
+
+ // 断开数据库连接
+ protected void disconnect() throws SQLException {
+ if (jdbcConnection != null && !jdbcConnection.isClosed()) {
+ jdbcConnection.close();
+ }
+ }
+
+
+ //封装添加数据的实例成员方法add()
+ public boolean add(Bill bill) throws SQLException {
+ String sql = "INSERT INTO bill (billDate,billMonery) VALUES (?,?)";
+ connect();
+ // 创建PreparedStatement对象
+ PreparedStatement statement = jdbcConnection.prepareStatement(sql);
+ statement.setString(1, bill.getBillDate());
+ statement.setDouble(2,bill.getBillMonery());
+ boolean rowInserted = statement.executeUpdate() > 0;
+ statement.close();
+ disconnect();
+ return rowInserted;
+ }
+
+
+ //封装查询数据的实例成员方法selectAll()
+ public List selectAll() throws SQLException{
+ ArrayList billArrayList = new ArrayList<>();
+ String sql = "SELECT billId,billDate,billMonery FROM bill";
+
+ connect();
+ Statement statement = jdbcConnection.createStatement();
+ ResultSet resultSet = statement.executeQuery(sql);
+ while (resultSet.next()) {
+ int billId = resultSet.getInt("billId");
+ String billDate = resultSet.getString("billDate");
+ Double billMonery = resultSet.getDouble("billMonery");
+ Bill bill = new Bill(billId,billDate,billMonery);
+ billArrayList.add(bill);
+ }
+ resultSet.close();
+ statement.close();
+ disconnect();
+ return billArrayList;
+ }
+
+}
diff --git a/src/main/java/org/example/domain/Bill.java b/src/main/java/org/example/domain/Bill.java
new file mode 100644
index 0000000..f171e3e
--- /dev/null
+++ b/src/main/java/org/example/domain/Bill.java
@@ -0,0 +1,28 @@
+package org.example.domain;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+
+/**
+ * 账单
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class Bill {
+ /**
+ * 账单编号
+ */
+ private Integer billId;
+ /**
+ * 账单时间
+ */
+ private String billDate;
+ /**
+ * 账单金额
+ */
+ private Double billMonery;
+}