Retrieve Database value to POJO class

 To retrieve data from an SQL database using a select query, process the `ResultSet`, and map the results to a POJO class in a Spring Boot application, you can follow these steps:


1. Set Up Database Configuration:

   Ensure you have configured the database connection in your Spring Boot application's `application.properties` or `application.yml` file.


2. Create the POJO Class:   

Create a POJO class that represents the structure of the data you'll retrieve from the database.


public class MyData {

    private String column1;

    private String column2;

    // ... other fields, getters, and setters

}


3. Retrieve Data Using JdbcTemplate:

   Use Spring's `JdbcTemplate` to execute the select query, process the `ResultSet`, and map the results to the POJO class.


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;


@Service

public class DataService {


    private final JdbcTemplate jdbcTemplate;


    @Autowired

    public DataService(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;

    }


    public MyData getDataFromDatabase() {

        String query = "SELECT column1, column2 FROM your_table_name WHERE your_condition LIMIT 1";


        return jdbcTemplate.query(query, rs -> {

            if (rs.next()) {

                MyData data = new MyData();

                data.setColumn1(rs.getString("column1"));

                data.setColumn2(rs.getString("column2"));

                // Set other fields as needed

                return data;

            }

            return null; // No data found

        });

    }

}


In the above code:

- Inject the `JdbcTemplate` bean into the `DataService` class.

- Use the `query` method of `JdbcTemplate` to execute the select query.

- Inside the lambda provided to the `query` method, process the `ResultSet` using `rs.next()` to iterate over the result rows.

- Map the retrieved data to the `MyData` POJO.


4. Controller to Expose Data:

   Create a Spring Controller to expose the retrieved data as an API endpoint.


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController

@RequestMapping("/api")

public class MyController {


    private final DataService dataService;


    @Autowired

    public MyController(DataService dataService) {

        this.dataService = dataService;

    }


    @GetMapping("/data")

    public MyData getData() {

        return dataService.getDataFromDatabase();

    }

}


5. Run the Spring Boot Application:

   Start your Spring Boot application, and you can access the data using the API endpoint, e.g., `http://localhost:8080/api/data`.


Make sure to replace `your_table_name` with the actual table name and `your_condition` with the appropriate SQL condition for selecting the desired row.


Please note that the above code assumes you are using Spring Boot's built-in `JdbcTemplate`. If you are using JPA or other ORM frameworks, the process might be slightly different, but the basic idea of querying the database, mapping results to a POJO, and exposing them through a Spring Controller remains the same.

Post a Comment

0 Comments