#1
This guide shows how to log SQL queries executed by Spring Boot when using the Apache Derby database for debugging and monitoring.

1. Add Dependencies

In pom.xml:
<dependency>
  <groupId>org.apache.derby</groupId>
  <artifactId>derby</artifactId>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2. Enable SQL Logging

In application.properties:
spring.datasource.url=jdbc:derby:memory:demoDB;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.jpa.hibernate.ddl-auto=update

# show SQL in console
spring.jpa.show-sql=true

# format queries for readability
spring.jpa.properties.hibernate.format_sql=true

# enable parameter binding logging
logging.level.org.hibernate.type.descriptor.sql=TRACE

3. Create Sample Entity

import jakarta.persistence.*;

@Entity
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    // getters and setters
}

4. Repository

import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

5. REST Controller

import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/customers")
public class CustomerController {
    private final CustomerRepository repo;

    public CustomerController(CustomerRepository repo) {
        this.repo = repo;
    }

    @PostMapping
    public Customer add(@RequestBody Customer c) {
        return repo.save(c);
    }

    @GetMapping
    public List<Customer> all() {
        return repo.findAll();
    }
}

6. Run and Check Logs

Start Spring Boot:
mvn spring-boot:run
When you hit endpoints, SQL queries will be printed:
curl -X POST http://localhost:8080/customers -H "Content-Type: application/json" -d '{"name":"Alice","email":"a@mail.com"}'
curl http://localhost:8080/customers
Console output shows generated SQL with parameter values.

image quote pre code