简体   繁体   中英

JdbcTemplate not working with Spring Boot project

and I'm using jdk8 and working on a SpringBoot project. I'm getting "JdbcTemplate cannot be resolved to a type" I tried to maven->update project,after updating the project,I'm getting error in @Repository annotation and asking to update the JDKto1.5. The error in JdbcTemplate have gone but not able use any methods using its reference.Kindly help!

package com.boot.rest.repository;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.boot.rest.model.User;

@Repository
public class UserService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<String> getUSerNames(){

        List<String> userlist = new ArrayList<String>();

        userlist.addAll(jdbcTemplate.queryForList("Select username from user"));

        return userlist;


    }
}

QueryForList return List<Map<String,Object>> not List<String> . Spring throw exception because you handle wrong type. Here example :

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#queryForList-java.lang.String-

And https://www.mkyong.com/spring/spring-jdbctemplate-querying-examples/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM