Home > Enterprise >  As ResultSetExtractor to implement SpringBoot
As ResultSetExtractor to implement SpringBoot

Time:12-13

I'm trying to create a ResultSetExtractor class but I'm not succeeding in it, the class is not compiling and I don't know why `

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContatoDao {
    private JdbcTemplate template;

    public List<Contato> setTemplate(JdbcTemplate template) {
        this.template = template;

        List<Contato> getAllContatoes;
        {
            return template.query("select * from CONTATO", new ResultSetExtractor<List<Contato>>() {

                @Override
                public List<Contato> extractData(ResultSet rs) throws SQLException,
                        DataAccessException {
                    List<Contato> list = new ArrayList<Contato>();
                    while (rs.next()) {
                        Contato e = new Contato();
                        e.setId(rs.getLong(1));
                        e.setNome(rs.getString(2));
                        e.setEmail(rs.getString(2));
                        e.setTelefone(rs.getNString(2));
                    }
                    return list;
                }

            });


        }

    }
}

` how do i make the right syntax for the ResultSetExtractor

CodePudding user response:

this is my application.properties `

spring.datasource.url:jdbc:firebirdsql:localhost/3050:C:/DB/DASHBOARD.FDB?sql_dialect=3&charSet=utf-8
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username:SYSDBA
spring.datasource.password:masterkey
spring.datasource.driver-class-name=org.firebirdsql.jdbc.FBDriver
spring.jpa.database-platform=org.hibernate.community.dialect.FirebirdDialect

spring.jpa.show-sql: true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.generate-ddl=true
spring.jpa.defer-datasource-initialization=true

CodePudding user response:

Here's how I think it should be coded. No compiler errors, according to IntelliJ:

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

public class ContatoDao {

    private static final String SELECT_ALL = "select * from CONTATO";

    private final JdbcTemplate jdbcTemplate;

    public ContatoDao(DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }

    public List<Contato> findAll() {
        return this.jdbcTemplate.query(SELECT_ALL, rs -> {
            List<Contato> result = new ArrayList<>();
            while (rs.next()) {
                Contato e = new Contato();
                e.setId(rs.getLong(1));
                e.setNome(rs.getString(2));
                e.setEmail(rs.getString(3));
                e.setTelefone(rs.getNString(4));
                result.add(e);
            }
            return result;
        });
    }
}

I would change that SELECT statement so it spells out the column names instead of the star notation. I'd use the column names in the result extraction instead of column numbers.

CodePudding user response:

How would I turn this Dao class into a mapping in @GetMapping?

import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

public class ContatoDao {

    private static final String SELECT_ALL = "select * from CONTATO";

    private final JdbcTemplate jdbcTemplate;

    public ContatoDao(DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }

    public List<Contato> findAll() {
        return this.jdbcTemplate.query(SELECT_ALL, rs -> {
            List<Contato> result = new ArrayList<>();
            while (rs.next()) {
                Contato e = new Contato();
                e.setId(rs.getLong(1));
                e.setNome(rs.getString(2));
                e.setEmail(rs.getString(3));
                e.setTelefone(rs.getNString(4));
                result.add(e);
            }
            return result;
        });
    }
}
  • Related