? Understanding JDBC: Java Database Connectivity ?
Ngo Tran Quoc Tuan (Liam)
? Software Developer | Java Spring boot | Database | Studies Performance Tuning at Wecommit Vi?t Nam. Let's connect!
Java Database Connectivity (JDBC) is an API that enables Java applications to interact with relational databases. It provides a standard interface for connecting to a database, executing queries, and retrieving results.
?? Key Components of JDBC
? Steps to Use JDBC
1. ?? Load the JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
2. ? Establish a Connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
3. ?? Create and Execute a Statement
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
4. ?? Process the Results
while (rs.next()) {
System.out.println(rs.getString("name"));
}
5. ?? Close the Connection
rs.close();
stmt.close();
con.close();
?? Types of JDBC Drivers
? When to Use JDBC in Spring Boot
? High Performance Requirements
JDBC allows direct interaction with the database without the overhead of an ORM like Hibernate.
?? Complex Queries and Stored Procedures
JDBC provides fine-grained control over query execution and result handling.
领英推荐
?? Lightweight Applications
Using JDBC can reduce dependency on heavier frameworks when basic CRUD operations are needed.
?? Legacy Systems
JDBC offers better compatibility when integrating with legacy databases.
?? Batch Processing
JDBC allows batch updates, improving performance for multiple queries.
?? Using JDBC in a Spring Boot Application
?? Step 1: Add Dependencies
Add these dependencies in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
?? Step 2: Configure DataSource
Configure database properties in application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
?? Step 3: Use JdbcTemplate
Spring Boot provides JdbcTemplate, simplifying JDBC operations:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class UserRepository {
private final JdbcTemplate jdbcTemplate;
public UserRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<User> findAll() {
String sql = "SELECT * FROM users";
return jdbcTemplate.query(sql, new UserRowMapper());
}
}
class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
return new User(rs.getInt("id"), rs.getString("name"), rs.getString("email"));
}
}
?? Step 4: Perform Database Operations
You can now call UserRepository.findAll() to retrieve data.
? Advantages of JDBC
?? Best Practices
JDBC remains an essential part of Java-based database applications, providing a flexible and reliable way to interact with relational databases. Spring Boot enhances JDBC usage by reducing configuration overhead and simplifying database interactions. ?
? Software Developer | Java Spring boot | Database | Studies Performance Tuning at Wecommit Vi?t Nam. Let's connect!
1 个月Thank you, very informative! You helped me understand JDBC better.
Senior Software Engineer | Java | Spring Boot | Micro Services | Fullstack Software Developer | Angular | AWS | TechLead
1 个月Very informative! Thanks for sharing! ??