初始化
commit
047661929f
|
@ -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
|
|
@ -0,0 +1,8 @@
|
||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="PROJECT" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="workspaceImportForciblyTurnedOn" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>billManager</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- MySQL JDBC 驱动 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.26</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- JUnit 测试框架 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- lombok依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.22</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -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<Bill> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Bill> selectAll() throws SQLException{
|
||||||
|
ArrayList<Bill> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue