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.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; import java.util.Map; import javax.activation.DataSource; import konditer.client.bean.ContactType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; /** * * @author */ @Repository("contactTypeDao") public class ContactTypeDao implements ContactTypeDaoInterface { 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 addContactType(int contactTypeId, String contactTypeName) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public ContactType getContactType(int contactTypeId) { String SQL_QUERY = "SELECT CONTACT_TYPE_ID, CONTACT_TYPE_NAME, TIMESTAMP " + "FROM contact_types " + "WHERE CONTACT_TYPE_ID = ? "; ContactType contType = (ContactType) jdbcTemplate.queryForObject(SQL_QUERY, new Object[] { contactTypeId }, new BeanPropertyRowMapper(ContactType.class)); return contType; } @Override public ContactType getContactTypeByName(String contactTypeName) { String SQL_QUERY = "SELECT CONTACT_TYPE_ID, CONTACT_TYPE_NAME, TIMESTAMP " + "FROM contact_types " + "WHERE CONTACT_TYPE_NAME = ? "; ContactType contType = (ContactType) jdbcTemplate.queryForObject(SQL_QUERY, new Object[] { contactTypeName }, new BeanPropertyRowMapper(ContactType.class)); return contType; } @Override public ArrayList<ContactType> getAllContactTypes() { String SQL_QUERY = "SELECT CONTACT_TYPE_ID, CONTACT_TYPE_NAME, TIMESTAMP " + "FROM contact_types " + "ORDER BY CONTACT_TYPE_NAME"; List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY); ArrayList<ContactType> contactTypeList = getContactTypesList(rows); return contactTypeList; } @Override public int updateContactType(int contactTypeId, String newContactTypeName) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int deleteContactType(int contactTypeId) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private ArrayList<ContactType> getContactTypesList(List<Map<String, Object>> rows) { ArrayList<ContactType> contactTypeList = new ArrayList<>(); for (Map row : rows) { ContactType contactType = new ContactType(); contactType.setContactTypeId(Integer.parseInt(String.valueOf(row.get("CONTACT_TYPE_ID")))); contactType.setContactTypeName(String.valueOf(row.get("CONTACT_TYPE_NAME"))); //// 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])); contactType.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime()); /// contactTypeList.add(contactType); } return contactTypeList; } @Override public ArrayList<ContactType> getTimestamp() { String SQL_QUERY = "SELECT CONTACT_TYPE_ID, TIMESTAMP " + "FROM contact_types "; List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY); ArrayList<ContactType> contactTypeList = new ArrayList<>(); for (Map row : rows) { ContactType contactType = new ContactType(); contactType.setContactTypeId(Integer.parseInt(String.valueOf(row.get("CONTACT_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])); contactType.setTimestamp(new GregorianCalendar(year, month, day, hour, minut, (int) second).getTime()); /// contactTypeList.add(contactType); } return contactTypeList; } }