0

I wrote this code:

Controller:

@Controller
@RequestMapping("/groups")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @RequestMapping
    public String list(Model model) {
        model.addAttribute("groups", groupService.getAllGroups());
        return "groups";
    }
}

dbConnect

    public class dbConnect {

    public dbConnect(){}

    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
    public void DatabaseConnection(){
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
        driverManagerDataSource.setUrl("jdbc:postgres://xxx");
        driverManagerDataSource.setUsername("x");
        driverManagerDataSource.setPassword("xss");
        this.jdbcTemplate=new JdbcTemplate(driverManagerDataSource);
    }
}

Group Repo Interface

public interface GroupRepository {

    List<Group> getAllGroups();
}

Group Repo Impl

@Repository
public class InMemoryGroupRepository implements GroupRepository {

    public InMemoryGroupRepository(){}

    private dbConnect data;

    //@Autowired
    public InMemoryGroupRepository(dbConnect dbConnect) {
        this.data = dbConnect;
    }


    public List<Group> getAllGroups() {
        return data.getJdbcTemplate().query("SELECT id_grupy, nazwa, id_egzaminatora, haslo, egzaminatorzy_id_egzaminatora FROM grupy", new RowMapper<Group>() {
            public Group mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                Group group = new Group();
                group.setId_grupy(rs.getInt(1));
                group.setNazwa(rs.getString(2));
                group.setId_egzaminatora(rs.getInt(3));
                group.setHaslo(rs.getString(4));
                group.setEgzaminatorzy_id_egzaminatora(rs.getInt(5));
                return group;
            }
        });
    }
}

Group Service Interface

public interface GroupService {
    List<Group> getAllGroups();
}

Group Service Impl

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupRepository groupRepository;

    public List<Group> getAllGroups() {
        return groupRepository.getAllGroups();
    }
}

When I try enter "/groups" I get this error: error

I think that the problem is that database connection cannot be established. We excluded almost every possible mistake. We tried to use ArrayList instead of using database and it worked well. How can i fix it?

5
  • What is the url of your database? I see your url is: ` driverManagerDataSource.setUrl("jdbc:postgres://xxx");` is it in correct syntax? Refer to this to know setup jdbc with postgre Commented Jun 8, 2017 at 22:43
  • Check the port, 5432 is default for Postgres Commented Jun 8, 2017 at 22:44
  • Did you try with @Bean instead @Autowired? Commented Jun 8, 2017 at 22:55
  • Perhaps it's a problem with connect to Heroku, because there is our database. I will try figure it out tommorow. Commented Jun 8, 2017 at 23:35
  • Without spring context file it is not possible to see what's wrong. Where are you calling DatabaseConnection? Commented Jun 9, 2017 at 0:13

1 Answer 1

1

You have an error in your DB configuration. dbConnect class isn't managed by Spring and because of that you get NPE because since it isn't injected into your repository. I suggest using configuration below:

@Configuration
class PersistenceConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgres://xxx");
        dataSource.setUsername("x");
        dataSource.setPassword("xss");

        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}

And repository implementation:

@Repository
public class GroupPostgresRepository implements GroupRepository {

    private final JdbcTemplate jdbcTemplate;

    public GroupPostgresRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Group> getAllGroups() {
        final String sql = "SELECT id_grupy, nazwa, id_egzaminatora, haslo, egzaminatorzy_id_egzaminatora FROM grupy";

        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            Group group = new Group();
            group.setId_grupy(rs.getInt(1));
            group.setNazwa(rs.getString(2));
            group.setId_egzaminatora(rs.getInt(3));
            group.setHaslo(rs.getString(4));
            group.setEgzaminatorzy_id_egzaminatora(rs.getInt(5));
            return group;
        });
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.