Java tutorial
/* * 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.onclave.testbench.jdbcTemplate.DAOSupport; import com.onclave.testbench.jdbcTemplate.QUERIES.QueryStatements; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; /** * * @author sajib */ @Repository public class StudentDAOImplementation implements IStudentDAO { private JdbcTemplate jdbcTemplate = null; @Autowired public StudentDAOImplementation(DataSource dataSource) { this.jdbcTemplate = (new JdbcTemplate(dataSource)); } @Override public boolean createNewStudent(final StudentPOJO student) { this.jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection .prepareStatement(QueryStatements.INSERT_STUDENT_SQL); preparedStatement.setString(1, student.getName()); preparedStatement.setString(2, student.getRollNo()); preparedStatement.setString(3, student.getEmail()); preparedStatement.setBoolean(4, student.isActive()); preparedStatement.setInt(5, student.getAge()); return preparedStatement; } }); return true; } @Override public List<StudentPOJO> queryAllStudents() { List<StudentPOJO> students = this.jdbcTemplate.query(QueryStatements.QUERY_ALL_STUDENTS_SQL, new RowMapper<StudentPOJO>() { @Override public StudentPOJO mapRow(ResultSet resultSet, int rowNum) throws SQLException { StudentPOJO student = new StudentPOJO(); student.setIdStudent(resultSet.getLong("IDStudents")); student.setName(resultSet.getString("studentName")); student.setRollNo(resultSet.getString("studentRollNo")); student.setEmail(resultSet.getString("studentEmail")); student.setActive(resultSet.getBoolean("studentActive")); student.setAge(resultSet.getInt("studentAge")); return student; } }); return students; } @Override public boolean deleteStudent(final long idStudent) { final int result = jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection .prepareStatement(QueryStatements.DELETE_STUDENT_BY_ID_SQL); preparedStatement.setLong(1, idStudent); return preparedStatement; } }); if (result > 0) { return true; } return false; } @Override public boolean changeActivityStatusOfStudent(final long idStudent, final boolean activityStatus) { final int result = jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection .prepareStatement(QueryStatements.UPDATE_STUDENT_ACTIVITY_SQL); preparedStatement.setBoolean(1, activityStatus); preparedStatement.setLong(2, idStudent); return preparedStatement; } }); if (result > 0) { return true; } return false; } @Override public boolean updateStudent(StudentPOJO newStudent, StudentPOJO studentDetails) { return false; } }