Some doubts about RowMapper use in JDBC in a Spring Framework application

后端 未结 4 1676
悲哀的现实
悲哀的现实 2021-02-07 10:17

I am studying how to execute query on a database using JDBC in Spring Framework.

I am following this tutorial: http://www.tutorialspoint.com/spring/spring_jdbc_example.h

4条回答
  •  不要未来只要你来
    2021-02-07 11:09

    Using RowMapper in Spring

    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import org.springframework.jdbc.core.RowMapper;
    
    public class RowsMap implements RowMapper{
    
        @Override
        public EmpPojo mapRow(ResultSet rs, int counts) throws SQLException {
            EmpPojo em=new EmpPojo();
            em.setEid(rs.getInt(1));
            em.setEname(rs.getString(2));
            em.setEsal(rs.getDouble(3));
    
            return em;
        }
    
    }
    
    Finally in Main class
    
    List lm=jt.query("select * from emps", new RowsMap());
    for(EmpPojo e:lm)
    {
        System.out.println(e.getEid()+" "+e.getEname()+" "+e.getEsal());
    }
    

提交回复
热议问题