×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: sumesh tg
Added: Sep 27, 2016 6:46 AM
Views: 9
Tags: example impl
sampl impl using spring
  1. /*
  2.  * Copyright ZoftSolutions(C) 2016 SUMESH T.G <ZoftSolutions>
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  *  Developed by ZoftSolutions (2015) Company.
  17.  */
  18. package com.zs.ina.search.master.subclass.dao;
  19.  
  20. import com.zs.ina.common.constants.TableNames;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.util.List;
  24. import javafx.collections.FXCollections;
  25. import javafx.collections.ObservableList;
  26. import javax.sql.DataSource;
  27. import org.apache.log4j.Logger;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.jdbc.core.JdbcTemplate;
  30. import org.springframework.jdbc.core.RowMapper;
  31.  
  32. /**
  33.  *
  34.  * @author SUMESH T.G <ZoftSolutions>
  35.  */
  36. public class SubClassIMPL implements SubClassDAO {
  37.  
  38.     static Logger logger = Logger.getLogger(SubClassIMPL.class);
  39.     @Autowired
  40.     DataSource dataSource;
  41.     private JdbcTemplate jdbcTemplate;
  42.  
  43.     @Override
  44.     public int insertSubClass(SubClassBEAN subClassBEAN) {
  45.         int row = 0;
  46.         jdbcTemplate = new JdbcTemplate(dataSource);
  47.  
  48.         String sql = "INSERT INTO " + TableNames.TBL_MSTR_SUB_CLASS + " ( id,sub_class)\n"
  49.                 + "VALUES\n"
  50.                 + "     (0, ?);";
  51.         row = jdbcTemplate.update(sql, new Object[]{subClassBEAN.getSubClass()});
  52.         return row;
  53.     }
  54.  
  55.     @Override
  56.     public int updateSubClass(SubClassBEAN subClassBEAN) {
  57.         int row = 0;
  58.         jdbcTemplate = new JdbcTemplate(dataSource);
  59.  
  60.         String sql = "UPDATE " + TableNames.TBL_MSTR_SUB_CLASS + "\n"
  61.                 + "SET \n"
  62.                 + " sub_class = ?\n"
  63.                 + "WHERE\n"
  64.                 + "     id=?";
  65.         row = jdbcTemplate.update(sql, new Object[]{subClassBEAN.getSubClass(), subClassBEAN.getId()});
  66.         return row;
  67.     }
  68.  
  69.     @Override
  70.     public int deleteSubClass(String id) {
  71.         int row = 0;
  72.         jdbcTemplate = new JdbcTemplate(dataSource);
  73.         String sql = "DELETE FROM " + TableNames.TBL_MSTR_SUB_CLASS
  74.                 + " WHERE id = ?\n";
  75.         row = jdbcTemplate.update(sql, new Object[]{id});
  76.         return row;
  77.     }
  78.  
  79.     @Override
  80.     public ObservableList<SubClassBEAN> retrieveSubClass( ) {
  81.         ObservableList<SubClassBEAN> listSubClasses = FXCollections.observableArrayList();
  82.         jdbcTemplate = new JdbcTemplate(dataSource);
  83.         String query = "SELECT * FROM " + TableNames.TBL_MSTR_SUB_CLASS + "";
  84.         List<SubClassBEAN> temp = jdbcTemplate.query(query, new SubClassMapper());
  85.         listSubClasses.addAll(temp);
  86.         return listSubClasses;
  87.     }
  88.     public class SubClassMapper implements RowMapper<SubClassBEAN> {
  89.  
  90.         @Override
  91.         public SubClassBEAN mapRow(ResultSet rs, int rowNum) throws SQLException {
  92.             SubClassBEAN subClassBEAN = new SubClassBEAN();
  93.             subClassBEAN.setId(rs.getString("id"));
  94.             subClassBEAN.setSubClass(rs.getString("sub_class"));
  95.             return subClassBEAN;
  96.         }
  97.  
  98.     }
  99. }
  100.