3

Hi guys I developing a spring application and got stuck in one place. What I am doing is fetching the data from the database using spring rowmapper. The problem is while implementing the rowmapper class Model class object is creating exactly the total number of rows times so, if table has 10 rows then 10 object of model class is created. I want to create only one object of that model class so I injected the model class in the dao class but the result is it returning the last row data only 10 times.

Model class

public class Item {
    private String ItemId;
    private String ItemName;
    private String price;

   // getter & setter
}

DAO class

public class Itemdao {
    private JdbcTemplate template;
    private Item items;

    public JdbcTemplate getTemplate() {
        return template;
    }

    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public List<Item> getItem(){

        return template.query("select * from item", new RowMapper<Item>(){

        @Override
            public Item mapRow(ResultSet rs, int rownum) throws SQLException                
            {
            //Item item = new Item(); // Using this line I get 10 objects of model
                items.setItemId(rs.getString(1));
                items.setItemName(rs.getString(2));
                items.setPrice(rs.getString(3));
                return items;
            }});
       }

    public Item getItems() {
        return items;
    }

    public void setItems(Item items) {
        this.items = items;
    }
   }

Main class

public class test {
     public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spconfig.xml");
        Itemdao dItemdao = (Itemdao)context.getBean("item");
        List<Item> list = dItemdao.getItem();
        for(Item i:list)
          System.out.println(i);
        }
    }

Spring configuration

<beans>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />  
    <property name="username" value="sandhya" />  
    <property name="password" value="2611798" />  
</bean>  

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
    <property name="dataSource" ref="ds"/>  
</bean>
<bean id="items" class="com.shopping.Item.Model.Item"/>
<bean id="item" class="com.shopping.Item.Model.Itemdao">
    <property name="template" ref="jdbcTemplate"/>
    <property name="items" ref="items"/>
</bean>

</beans>
Vivek Singh
  • 646
  • 3
  • 10
  • 25
  • And what would you expect? If you have 10 rows in db and you want to have just one item in the list? Either update your query to get the single row that you expect (distict keyword maybe could help, or some additions to where clause) or deal with duplicates after fetching and mapping the data. – Nadir Oct 20 '16 at 11:51
  • I want the complete row data. Using first approach i.e using new Item() I am getting all data as I want but Object of model class is also creating no. of rows times. there is no problem of duplicate. I just want to create only one object of Model class. – Vivek Singh Oct 20 '16 at 11:54
  • So your model class should contain a list of items not be an item, see @Sundararaj Govindasamy answer, should be helpful – Nadir Oct 20 '16 at 11:57
  • You really should make your dao method result a list of objects as it is now. When using select query you can't be sure that the query will result always and in every case only one row of data => one object. jdbcTemplate.queryForObject() will create only one object but if the query results 0, 2 or more rows, it will cause an error (Exception) and break your app. – micaro Oct 20 '16 at 12:24

1 Answers1

3

Use queryForObject() instead query(), See this java doc of JDBCTemplate class for all available APIs.

Refer this sample,

public Customer findByCustomerId(int custId){

    String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";

    Customer customer = (Customer)getJdbcTemplate().queryForObject(
            sql, new Object[] { custId }, new CustomerRowMapper());

    return customer;
}
Sundararaj Govindasamy
  • 8,180
  • 5
  • 44
  • 77