This guide shows how to connect
Spring Boot to an
Apache Derby Network Server, running Derby in client/server mode instead of embedded.
1. Start Derby Network Server
Run Derby network server from command line:
java -jar $DERBY_HOME/lib/derbynet.jar start -p 1527
Default port is
1527
.
Create a database:
java -jar $DERBY_HOME/lib/ij.jar
Inside IJ tool:
CONNECT 'jdbc:derby://localhost:1527/demoDB;create=true' USER 'user' PASSWORD 'pass';
2. Add Dependencies
In
pom.xml
:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
3. Configure Spring Boot
In
application.properties
:
spring.datasource.url=jdbc:derby://localhost:1527/demoDB;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.ClientDriver
spring.datasource.username=user
spring.datasource.password=pass
spring.jpa.hibernate.ddl-auto=update
4. Define 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
}
5. Repository
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
6. 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 customer) {
return repo.save(customer);
}
@GetMapping
public List<Customer> all() {
return repo.findAll();
}
}
7. Run and Test
Start Derby Network Server, then run Spring Boot:
mvn spring-boot:run
Test APIs:
curl -X POST http://localhost:8080/customers -H "Content-Type: application/json" -d '{"name":"John","email":"john@mail.com"}'
curl http://localhost:8080/customers
image quote pre code