I'm developing java,spring web application. I had some problems with adding DataSource to the DAO implement class which extends JdbcDaoSupport.
I searched through the internet and found similar solution.
DaoImpliment Class
@Repository
public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {
@Autowired
private DataSource dataSource;
@PostConstruct
private void initialize() {
setDataSource(dataSource);
}
public int getUserID(String userName) {
//testing JDBC
String sql = "select user_id from users where username =" + "'" + userName + "'";
List<UserDTO> userDTOs = new ArrayList<UserDTO>();
List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
for (Map row : rows) {
UserDTO userDTO = new UserDTO();
userDTO.setUserID((Integer) row.get("CUST_ID"));
userDTOs.add(userDTO);
}
int userID = userDTOs.get(0).getUserID();
return userID;
}
}
and my bean configaration.
<bean id="userDao" class="com.avers.dao.UserDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/aversdb"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
but this gives me the error. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
I think the error is in bean configuration. I'm new to spring and someone please help.