基于java健身房管理系统的编程
时间: 2023-12-01 08:04:47 浏览: 64
基于java的健身房管理系统设计与实现(源代码+数据库+部署文档+辅导视频).zip
基于Java的健身房管理系统可以实现会员信息的管理,健身计划的制定和管理,器材的管理,教练的管理等功能。具体实现步骤如下:
1. 设计数据库,包括会员信息表,健身计划表,器材表,教练表等。
```sql
CREATE TABLE member (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
age INT,
phone VARCHAR(20),
email VARCHAR(50),
address VARCHAR(100)
);
CREATE TABLE fitness_plan (
id INT PRIMARY KEY,
name VARCHAR(50),
description VARCHAR(200),
create_time TIMESTAMP,
update_time TIMESTAMP
);
CREATE TABLE equipment (
id INT PRIMARY KEY,
name VARCHAR(50),
type VARCHAR(50),
price DECIMAL(10, 2),
purchase_time TIMESTAMP,
status VARCHAR(20)
);
CREATE TABLE coach (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
age INT,
phone VARCHAR(20),
email VARCHAR(50),
address VARCHAR(100),
salary DECIMAL(10, 2),
hire_date TIMESTAMP
);
```
2. 使用Java连接数据库,实现对数据库的增删改查操作。
```java
import java.sql.*;
public class Database {
private static final String URL = "jdbc:mysql://localhost:3306/fitness?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 实现会员信息的管理,包括添加会员,删除会员,修改会员信息,查询会员信息等操作。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MemberDao {
public void add(Member member) {
String sql = "INSERT INTO member(id, name, gender, age, phone, email, address) VALUES(?, ?, ?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, member.getId());
stmt.setString(2, member.getName());
stmt.setString(3, member.getGender());
stmt.setInt(4, member.getAge());
stmt.setString(5, member.getPhone());
stmt.setString(6, member.getEmail());
stmt.setString(7, member.getAddress());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public void delete(int id) {
String sql = "DELETE FROM member WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public void update(Member member) {
String sql = "UPDATE member SET name=?, gender=?, age=?, phone=?, email=?, address=? WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setString(1, member.getName());
stmt.setString(2, member.getGender());
stmt.setInt(3, member.getAge());
stmt.setString(4, member.getPhone());
stmt.setString(5, member.getEmail());
stmt.setString(6, member.getAddress());
stmt.setInt(7, member.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public Member findById(int id) {
String sql = "SELECT * FROM member WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Member member = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
member = new Member();
member.setId(rs.getInt("id"));
member.setName(rs.getString("name"));
member.setGender(rs.getString("gender"));
member.setAge(rs.getInt("age"));
member.setPhone(rs.getString("phone"));
member.setEmail(rs.getString("email"));
member.setAddress(rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return member;
}
public List<Member> findAll() {
String sql = "SELECT * FROM member";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Member> members = new ArrayList<>();
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Member member = new Member();
member.setId(rs.getInt("id"));
member.setName(rs.getString("name"));
member.setGender(rs.getString("gender"));
member.setAge(rs.getInt("age"));
member.setPhone(rs.getString("phone"));
member.setEmail(rs.getString("email"));
member.setAddress(rs.getString("address"));
members.add(member);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return members;
}
}
```
4. 实现健身计划的制定和管理,包括添加健身计划,删除健身计划,修改健身计划信息,查询健身计划信息等操作。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class FitnessPlanDao {
public void add(FitnessPlan fitnessPlan) {
String sql = "INSERT INTO fitness_plan(id, name, description, create_time, update_time) VALUES(?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, fitnessPlan.getId());
stmt.setString(2, fitnessPlan.getName());
stmt.setString(3, fitnessPlan.getDescription());
stmt.setTimestamp(4, new Timestamp(fitnessPlan.getCreateTime().getTime()));
stmt.setTimestamp(5, new Timestamp(fitnessPlan.getUpdateTime().getTime()));
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public void delete(int id) {
String sql = "DELETE FROM fitness_plan WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public void update(FitnessPlan fitnessPlan) {
String sql = "UPDATE fitness_plan SET name=?, description=?, update_time=? WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setString(1, fitnessPlan.getName());
stmt.setString(2, fitnessPlan.getDescription());
stmt.setTimestamp(3, new Timestamp(fitnessPlan.getUpdateTime().getTime()));
stmt.setInt(4, fitnessPlan.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public FitnessPlan findById(int id) {
String sql = "SELECT * FROM fitness_plan WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
FitnessPlan fitnessPlan = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
fitnessPlan = new FitnessPlan();
fitnessPlan.setId(rs.getInt("id"));
fitnessPlan.setName(rs.getString("name"));
fitnessPlan.setDescription(rs.getString("description"));
fitnessPlan.setCreateTime(rs.getTimestamp("create_time"));
fitnessPlan.setUpdateTime(rs.getTimestamp("update_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return fitnessPlan;
}
public List<FitnessPlan> findAll() {
String sql = "SELECT * FROM fitness_plan";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<FitnessPlan> fitnessPlans = new ArrayList<>();
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
FitnessPlan fitnessPlan = new FitnessPlan();
fitnessPlan.setId(rs.getInt("id"));
fitnessPlan.setName(rs.getString("name"));
fitnessPlan.setDescription(rs.getString("description"));
fitnessPlan.setCreateTime(rs.getTimestamp("create_time"));
fitnessPlan.setUpdateTime(rs.getTimestamp("update_time"));
fitnessPlans.add(fitnessPlan);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return fitnessPlans;
}
}
```
5. 实现器材的管理,包括添加器材,删除器材,修改器材信息,查询器材信息等操作。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class EquipmentDao {
public void add(Equipment equipment) {
String sql = "INSERT INTO equipment(id, name, type, price, purchase_time, status) VALUES(?, ?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, equipment.getId());
stmt.setString(2, equipment.getName());
stmt.setString(3, equipment.getType());
stmt.setBigDecimal(4, equipment.getPrice());
stmt.setTimestamp(5, new Timestamp(equipment.getPurchaseTime().getTime()));
stmt.setString(6, equipment.getStatus());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public void delete(int id) {
String sql = "DELETE FROM equipment WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public void update(Equipment equipment) {
String sql = "UPDATE equipment SET name=?, type=?, price=?, purchase_time=?, status=? WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setString(1, equipment.getName());
stmt.setString(2, equipment.getType());
stmt.setBigDecimal(3, equipment.getPrice());
stmt.setTimestamp(4, new Timestamp(equipment.getPurchaseTime().getTime()));
stmt.setString(5, equipment.getStatus());
stmt.setInt(6, equipment.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, null);
}
}
public Equipment findById(int id) {
String sql = "SELECT * FROM equipment WHERE id=?";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Equipment equipment = null;
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
equipment = new Equipment();
equipment.setId(rs.getInt("id"));
equipment.setName(rs.getString("name"));
equipment.setType(rs.getString("type"));
equipment.setPrice(rs.getBigDecimal("price"));
equipment.setPurchaseTime(rs.getTimestamp("purchase_time"));
equipment.setStatus(rs.getString("status"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return equipment;
}
public List<Equipment> findAll() {
String sql = "SELECT * FROM equipment";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Equipment> equipments = new ArrayList<>();
try {
conn = Database.getConnection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Equipment equipment = new Equipment();
equipment.setId(rs.getInt("id"));
equipment.setName(rs.getString("name"));
equipment.setType(rs.getString("type"));
equipment.setPrice(rs.getBigDecimal("price"));
equipment.setPurchaseTime(rs.getTimestamp("purchase_time"));
equipment.setStatus(rs.getString("status"));
equipments.add(equipment);
}
} catch (
阅读全文