用户可通过该项目学习 Java 连接 MySQL 数据库及执行 CRUD 操作的基础方法,包含传统手动资源管理和 try-with-resources 自动管理两种连接方式,清晰演示插入、查询、更新、删除等核心操作。【此简介由AI生成】
Access rate limit exceeded. Please log in to continue.
Basic JDBC Project
Overview
This is a basic Java JDBC (Java Database Connectivity) learning project that demonstrates how to connect to a MySQL database using the JDBC API and perform basic CRUD (Create, Read, Update, Delete) operations.
Technology Stack
- Java: JDK 26
- Database: MySQL
- JDBC Driver: MySQL Connector/J 9.4.0
- Build Tool: Maven
Project Structure
basic-jdbc/
├── src/main/java/
│ ├── connection/ # Database connection examples
│ │ ├── BasicJDBC.java # Basic connection (manual resource closing)
│ │ └── AutoJDBC.java # Automatic resource management (try-with-resources)
│ └── CRUD/ # CRUD operation examples
│ ├── Create.java # Insert data
│ ├── Read.java # Query data
│ ├── Update.java # Update data
│ └── Delete.java # Delete data
└── pom.xml # Maven configuration file
Prerequisites
- JDK 26 or higher
- MySQL database (this project uses the sakila sample database)
- Maven 3.x
Configuration
Database Connection Settings
All examples use the following default configuration:
- Host: 127.0.0.1 (localhost)
- Port: 3306
- Database: sakila
- Username: root
- Password: root
- Encoding: UTF-8
Note: Please modify the connection parameters according to your MySQL configuration before use.
Running the Examples
1. Compile the Project
mvn clean compile
2. Run Individual Examples
# Run basic connection example
mvn exec:java -Dexec.mainClass="connection.BasicJDBC"
# Run automatic resource management example
mvn exec:java -Dexec.mainClass="connection.AutoJDBC"
# Run CRUD operations
mvn exec:java -Dexec.mainClass="CRUD.Create"
mvn exec:java -Dexec.mainClass="CRUD.Read"
mvn exec:java -Dexec.mainClass="CRUD.Update"
mvn exec:java -Dexec.mainClass="CRUD.Delete"
Features
Database Connection
BasicJDBC.java
Demonstrates the traditional JDBC connection approach, requiring manual closing of Statement and Connection resources in a finally block.
AutoJDBC.java
Demonstrates the modern approach using try-with-resources statements for automatic resource management, resulting in cleaner and safer code.
CRUD Operations
Create.java
Inserts new records into the actor table, demonstrating INSERT operations.
Read.java
Queries all records from the actor table, demonstrating SELECT operations and ResultSet iteration.
Update.java
Updates records in the actor table, demonstrating UPDATE operations.
Delete.java
Deletes records from the actor table, demonstrating DELETE operations.
Known Issues and Improvement Suggestions
⚠️ Security Issues
- SQL Injection Risk: All examples use Statement with concatenated SQL, which is vulnerable to SQL injection. PreparedStatement should be used instead.
- Hardcoded Credentials: Database username and password are hardcoded in the source code. Configuration files or environment variables should be used.
🔧 Code Quality Issues
-
Improper Exception Handling:
- Exceptions are only printed without proper error handling
- BasicJDBC throws RuntimeException directly, causing program crashes
-
Resource Leak Risk: The resource closing approach in BasicJDBC is verbose and error-prone
-
Missing Return Value Validation: The return value of execute() method is not checked
💡 Best Practice Recommendations
-
Missing Transaction Management: Transaction management should be considered for write operations
-
Hardcoded SQL: SQL statements should be externalized to configuration files
-
Missing Logging Framework: Using System.out.println and printStackTrace; SLF4J + Logback is recommended
Improvement Directions
- Use PreparedStatement to prevent SQL injection
- Introduce configuration files for database connection parameters
- Add proper logging
- Implement connection pooling (e.g., HikariCP)
- Add unit tests
- Use ORM frameworks (e.g., MyBatis or JPA/Hibernate)
License
This project is for learning and educational purposes only.
Contact
Feel free to open an issue if you have any questions or suggestions.