com.leapfrog.academyspring.dao.impl.CourseDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.leapfrog.academyspring.dao.impl.CourseDAOImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.leapfrog.academyspring.dao.impl;

import com.leapfrog.academyspring.constants.SQLConstants;
import com.leapfrog.academyspring.dao.CourseDAO;
import com.leapfrog.academyspring.entity.Course;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Suman Heuju
 */
@Repository(value = "CourseDAO")

public class CourseDAOImpl implements CourseDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private Course mapData(ResultSet rs) throws SQLException {
        Course cor = new Course();

        cor.setId(rs.getInt("id"));
        cor.setName(rs.getString("name"));
        cor.setDescription(rs.getString("description"));
        cor.setCourseDuration(rs.getInt("course_duration"));
        cor.setDurationType(rs.getString("duration_type"));
        cor.setFees(rs.getDouble("fees"));
        cor.setAddedDate(rs.getDate("added_date"));
        cor.setModifiedDate(rs.getDate("modified_date"));
        cor.setDeleteFlag(rs.getBoolean("delete_flag"));
        cor.setDeleteDate(rs.getDate("delete_date"));
        cor.setStatus(rs.getBoolean("status"));

        return cor;
    }

    @Override
    public int insert(Course c) {
        return jdbcTemplate.update(SQLConstants.COURSE_INSERT, new Object[] { c.getName(), c.getDescription(),
                c.getCourseDuration(), c.getDurationType(), c.getFees(), c.isStatus() });
    }

    @Override
    public int update(Course c) {
        return jdbcTemplate.update(SQLConstants.COURSE_UPDATE, new Object[] { c.getId(), c.getName(),
                c.getDescription(), c.getCourseDuration(), c.getDurationType(), c.getFees(), c.isStatus() });
    }

    @Override
    public int delete(int id) {
        return jdbcTemplate.update(SQLConstants.COURSE_DELETE, new Object[] { id });
    }

    @Override
    public List<Course> getAll() {

        return jdbcTemplate.query(SQLConstants.COURSE_GETALL, new RowMapper<Course>() {

            @Override
            public Course mapRow(ResultSet rs, int i) throws SQLException {
                return mapData(rs);
            }
        });

    }

    @Override
    public Course getById(int id) {
        return jdbcTemplate.queryForObject(SQLConstants.COURSE_GETBYID, new Object[] { id },
                new RowMapper<Course>() {

                    @Override
                    public Course mapRow(ResultSet rs, int i) throws SQLException {
                        return mapData(rs);
                    }
                });
    }

}