Spring jdbcTemplate.queryForObject example


      Click here to attend Spring Framework 4.x and certification course with a discount

public User getUserByUsername(String email) {

        String SQL = "SELECT iduser,firstname,lastname,dob,mobile1,mobile2," +
        		" homephone,gender,country,student,working,issmoker,haspets,interests,description,schoolname,lastlogin,education,photopath" +
        		" FROM kameroom.user"
                + " WHERE email = ?";
        try {
            //using anonymous inner class RowMapper here
            return (User) jdbcTemplate.queryForObject(SQL, new RowMapper<User>() {
                public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                    User user = new User();
                    user.setIdUser(rs.getInt("iduser"));
                    user.setFirstname(rs.getString("firstname"));
                    user.setLastname(rs.getString("lastname"));
                    user.setDob(rs.getDate("dob"));
                    user.setMobile1(rs.getString("mobile1"));
                    user.setMobile2(rs.getString("mobile2"));
                    user.setHomephone(rs.getString("homephone"));
                    user.setGender(rs.getString("gender"));
                    user.setCountry(rs.getString("country"));
                    user.setStudent(rs.getBoolean("student"));
                    user.setWorking(rs.getBoolean("working"));
                    user.setSmoker(rs.getBoolean("issmoker"));
                    user.setHaspets(rs.getBoolean("haspets"));
                    user.setInterests(rs.getString("interests"));
                    user.setDescription(rs.getString("description"));
                    user.setSchoolname(rs.getString("schoolname"));
                    user.setEducation(rs.getString("education"));
                    user.setLastlogin(rs.getTimestamp("lastlogin"));
                    user.setPhotopath(rs.getString("photopath"));
                    return user;
                }
            }, new Object[]{email});
        } catch (EmptyResultDataAccessException ex) {
            return null;
        }
    }   
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s