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 konditer.client.dao; import java.sql.Types; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.activation.DataSource; import konditer.client.bean.CakeAndType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; /** * * @author */ @Repository("cakeAndTypeDao") public class CakeAndTypeDao implements CakeAndTypeDaoInterface { private DataSource dataSource = null; private JdbcTemplate jdbcTemplate = null; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } @Autowired public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void addCakeAndType(int cakeAndTypeId, int cakeId, int cakeTypeId) { String SQL_QUERY = "INSERT INTO cakes_and_types ( CAKE_AND_TYPE_ID, " + "CAKE_ID, " + "CAKE_TYPE_ID ) " + "VALUES (?,?,?)"; try { int rowCount = jdbcTemplate.update(SQL_QUERY, new Object[] { cakeAndTypeId, cakeId, cakeTypeId }, new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER }); Logger.getLogger(CakeDao.class.getName()).log(Level.SEVERE, " {0} ? {1} .", new Object[] { cakeId, rowCount }); } catch (DataAccessException e) { Logger.getLogger(CakeDao.class.getName()).log(Level.SEVERE, ". {0} .", cakeId); } } @Override public CakeAndType getCakeAndType(int cakeAndTypeId) { String SQL_QUERY = "SELECT CAKE_AND_TYPE_ID, CAKE_ID, CAKE_TYPE_ID, TIMESTAMP " + "FROM cakes_and_types " + "WHERE CAKE_AND_TYPE_ID = ?"; CakeAndType cakeAndType = (CakeAndType) jdbcTemplate.queryForObject(SQL_QUERY, new Object[] { cakeAndTypeId }, new BeanPropertyRowMapper(CakeAndType.class)); return cakeAndType; } @Override public ArrayList<CakeAndType> getAllCakesAndTypes() { String SQL_QUERY = "SELECT CAKE_AND_TYPE_ID, CAKE_ID, CAKE_TYPE_ID, TIMESTAMP " + "FROM cakes_and_types "; List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY); ArrayList<CakeAndType> cakeAndTypeList = getCakesAndTypesList(rows); return cakeAndTypeList; } @Override public ArrayList<CakeAndType> getAllCakesAndTypesByCakeId(int cakeId) { String SQL_QUERY = "SELECT CAKE_AND_TYPE_ID, CAKE_ID, CAKE_TYPE_ID, TIMESTAMP " + "FROM cakes_and_types " + "WHERE CAKE_ID = ? "; List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY, new Object[] { cakeId }); ArrayList<CakeAndType> cakeAndTypeList = getCakesAndTypesList(rows); return cakeAndTypeList; } @Override public ArrayList<CakeAndType> getAllCakesAndTypesByCakeTypeId(int cakeTypeId) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public ArrayList<CakeAndType> searchCakesAndTypes(String sqlQuery) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int updateCakeAndType(int cakeAndTypeId, int newCakeId, int newCakeTypeId) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int deleteCakeAndType(int cakeAndTypeId) { String SQL_QUERY = "DELETE FROM cakes_and_types WHERE CAKE_AND_TYPE_ID = ? "; int rowCount = jdbcTemplate.update(SQL_QUERY, new Object[] { cakeAndTypeId }, new int[] { Types.INTEGER }); Logger.getLogger(CakeAndTypeDao.class.getName()).log(Level.SEVERE, " : {0} .", rowCount); return rowCount; } private ArrayList<CakeAndType> getCakesAndTypesList(List<Map<String, Object>> rows) { ArrayList<CakeAndType> cakeAndTypeList = new ArrayList<>(); for (Map row : rows) { CakeAndType cakeAndType = new CakeAndType(); cakeAndType.setCakeAndTypeId(Integer.parseInt(String.valueOf(row.get("CAKE_AND_TYPE_ID")))); cakeAndType.setCakeId(Integer.parseInt(String.valueOf(row.get("CAKE_ID")))); cakeAndType.setCakeTypeId(Integer.parseInt(String.valueOf(row.get("CAKE_TYPE_ID")))); //// String[] dateArrFull = (String.valueOf(row.get("TIMESTAMP"))).split(" "); String[] dateArrDate = dateArrFull[0].split("-"); int year = Integer.parseInt(String.valueOf(dateArrDate[0]))/* -1900 */; int month = Integer.parseInt(String.valueOf(dateArrDate[1])) - 1; int day = Integer.parseInt(String.valueOf(dateArrDate[2])); String[] dateArrTime = dateArrFull[1].split(":"); int hour = Integer.parseInt(String.valueOf(dateArrTime[0])); int minut = Integer.parseInt(String.valueOf(dateArrTime[1])); double second = Double.parseDouble(String.valueOf(dateArrTime[2])); cakeAndType.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime()); /// cakeAndTypeList.add(cakeAndType); } return cakeAndTypeList; } @Override public ArrayList<CakeAndType> getTimestamp() { String SQL_QUERY = "SELECT CAKE_AND_TYPE_ID, TIMESTAMP " + "FROM cakes_and_types "; List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY); ArrayList<CakeAndType> cakeAndTypeList = new ArrayList<>(); for (Map row : rows) { CakeAndType cakeAndType = new CakeAndType(); cakeAndType.setCakeAndTypeId(Integer.parseInt(String.valueOf(row.get("CAKE_AND_TYPE_ID")))); //// String[] dateArrFull = (String.valueOf(row.get("TIMESTAMP"))).split(" "); String[] dateArrDate = dateArrFull[0].split("-"); int year = Integer.parseInt(String.valueOf(dateArrDate[0]))/* -1900 */; int month = Integer.parseInt(String.valueOf(dateArrDate[1])) - 1; int day = Integer.parseInt(String.valueOf(dateArrDate[2])); String[] dateArrTime = dateArrFull[1].split(":"); int hour = Integer.parseInt(String.valueOf(dateArrTime[0])); int minut = Integer.parseInt(String.valueOf(dateArrTime[1])); double second = Double.parseDouble(String.valueOf(dateArrTime[2])); cakeAndType.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime()); /// cakeAndTypeList.add(cakeAndType); } return cakeAndTypeList; } }