-1

I have this exception in my code. I am new to Java. Below I mention my code.

I need to know why this happens even if I gave value to name parameter.

I changed my parameter several times but it gives me same exception.

I use Springframework 4.3.7 spring-context, spring-webmvc, spring-jdbc.

This my UserDaoImpl code

   package com.ezeon.capp.dao;

   import com.ezeon.capp.domain.User;
   import java.util.HashMap;
   import java.util.List;
   import java.util.Map;
   import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
   import org.springframework.jdbc.core.namedparam.SqlParameterSource;
   import org.springframework.jdbc.support.GeneratedKeyHolder;
   import org.springframework.jdbc.support.KeyHolder;
   import org.springframework.stereotype.Repository;


@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {

@Override
 public void save(User u) {
        String sql =" INSERT INTO user(name, phone, email, address, loginName, 
        password, role, loginStatus)"
        +" VALUES ( :name, :phone, :email, :address, :loginName, :password, 
 :role, :logingStatus)";
        Map m = new HashMap();

        m.put("name", u.getName());
        m.put("phone", u.getPhone());
        m.put("email", u.getEmail());
        m.put("address", u.getAddress());
        m.put("loginName", u.getLoginName());
        m.put("password", u.getPassword());
        m.put("role", u.getRole());
        m.put("loginStatus", u.getLoginStatus());

        KeyHolder kh = new GeneratedKeyHolder();
        SqlParameterSource ps = new MapSqlParameterSource();
        super.getNamedParameterJdbcTemplate().update(sql, ps, kh);
        Integer userId = kh.getKey().intValue();
        u.setUserId(userId);
    }

}

This is my Test class

package com.ezeon.capp.test;

import com.ezeon.capp.config.SpringRootConfig;
import com.ezeon.capp.dao.IUserDao;
import com.ezeon.capp.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class TestUserDaoSave {
    public static void main(String[] args){

        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringRootConfig.class);

        IUserDao userdao = ctx.getBean(IUserDao.class);
        User u = new User();

        u.setName("Tharinda");
        u.setPhone("0714536598");
        u.setEmail("tharind@gmail.com");
        u.setAddress("Kurunegala");
        u.setLoginName("amit");
        u.setPassword("123");
        u.setRole(1);
        u.setLoginStatus(1);

        userdao.save(u);
        System.out.println("-------------Saved-----------");
    }

}

Exception

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'name': No value registered for key 'name'
    at org.springframework.jdbc.core.namedparam.NamedParameterUtils.buildValueArray(NamedParameterUtils.java:342)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:309)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:299)
    at com.ezeon.capp.dao.UserDaoImpl.save(UserDaoImpl.java:42)
    at com.ezeon.capp.test.TestUserDaoSave.main(TestUserDaoSave.java:35)
halfer
  • 18,701
  • 13
  • 79
  • 158
tharinda
  • 9
  • 6

1 Answers1

0

Your code looks fine, you have created a map for named parameters. But you haven't really used it.

Replace:

SqlParameterSource ps = new MapSqlParameterSource();

with

SqlParameterSource ps = new MapSqlParameterSource(m);

Refer this doc

Himanshu Bhardwaj
  • 3,798
  • 2
  • 14
  • 35