JDBC is an API that enables applications to interact with databases in a standardized way. It provides a set of classes and interfaces to connect to a database, execute queries, and retrieve results, regardless of the underlying database management system.
java.sql and javax.sql pacakges.DriverManager: Manages database connectionsConnection: Represents a connection to a databaseStatement/PreparedStatement: Executes SQL queriesResultSet: Retrieves results of a queryimport java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String password = "password";
try {
// Load MySQL driver (optional with JDBC 4.0+)
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
Connection connection = DriverManager.getConnection(url, user, password);
// Create a statement
Statement statement = connection.createStatement();
// Execute query
ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
// Process results
while (resultSet.next()) {
System.out.println("ID: " + resultSet.getInt("id"));
System.out.println("Name: " + resultSet.getString("name"));
}
// Close resources
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
While JDBC is a foundational API for database interaction in Java, it has several drawbacks, especially for large or complex applications:
Spring Data JPA is a part of the Spring Framework and is built on top of Java Persistence API (JPA). It addresses many of the limitations of JDBC by providing a higher-level, ORM(Object-Relational Mapping) approach to database interaction.


user table which has the following columns: id, name, age, email.id is the primary key and it is configured to be auto-incrementing.age and email have respective default values of '1' and ''.user table is filled with rows as following:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity(name = "user") //Indicates that UserEntity object will be mapped to "user" table
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; //primary key
private String name;
private Integer age;
private String email;
}
@Entity(name = "user") annotation is a JPA annotation indicating that this class is a JPA entity and will be mapped to a database table. name = "user" specifies the name of the table in the database. If omitted, the default table name would be the class name (user_entity in this case, depending on naming conventions).@Id marks the field id as the primary key of the entity.@GeneratedValue(strategy = GenerationType.IDENTITY):id field will be automatically generated by the database. IDENTITY strategy means that the database will handle primary key generation, typically using an auto-increment column.public interface UserRepository extends JpaRepository<UserEntity, Long> {}
repository interface that extends JpaRepository, provided by Spring Data JPA.JpaRepository<UserEntity, Long> defines that this repository is for managing UserEntity objects, with the primary key id of type Long. findAll() retrieves all rows from the user table and maps them to UserEntity objects.save() persists a new UserEntity into the database or updates an existing one.findAll(), Spring Data JPA delegates to the underlying JPA implementation.SELECT * FROM user) via JDBC, then maps the result set to UserEntity objects.@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserApiController {
private final UserRepository userRepository;
@GetMapping("/find-all")
public List<UserEntity> findAll(){
return userRepository.findAll();
}
@GetMapping("/name")
public void autoSave(
@RequestParam String name
){
var user = UserEntity.builder()
.name(name)
.build();
userRepository.save(user);
}
}
GET request with http://localhost:8080/api/user/find-all, we get:[
{
"id": 1,
"name": "Hong Gil Dong",
"age": 10,
"email": "hong@gmail.com"
},
{
"id": 2,
"name": "Choi Seunghwan",
"age": 20,
"email": "KJUNK@gmail.com"
},
{
"id": 3,
"name": "Jonathan Kim",
"age": 30,
"email": "johnkim@naver.com"
}
]
GET request with http://localhost:8080/api/user/name with query parameter name = Hanni Pham, we can see that our table was updated as following:
