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_reorganized_database.dao; import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.sql.DataSource; import konditer_reorganized_database.bean.GenId; 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 Aspy */ @Repository public class GenIdDao { 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; } private int newId = 0; public GenIdDao() { } public int getCakeAndInsideId() throws SQLException { return getId("cakes_and_insides"); } public int getCakeInsideId() throws SQLException { return getId("cake_insides"); } public int getCakePriceId() throws SQLException { return getId("cake_prices"); } public int getCakeTypeId() throws SQLException { return getId("cake_types"); } public int getCakeId() throws SQLException { return getId("cakes"); } public int getCakeAndTypeId() throws SQLException { return getId("cakes_and_types"); } public int getContactTypeId() throws SQLException { return getId("contact_types"); } public int getContactId() throws SQLException { return getId("contacts"); } public int getCustomerId() throws SQLException { return getId("customers"); } public int getDeliveryId() throws SQLException { return getId("deliveries"); } public int getDiscountId() throws SQLException { return getId("discounts"); } public int getOrderId() throws SQLException { return getId("orders"); } public int getPageMetadataId() throws SQLException { return getId("page_metadatas"); } public int getSitePageId() throws SQLException { return getId("site_pages"); } public int getUserId() throws SQLException { return getId("users"); } public int getId(String tableName) throws SQLException { String SQL_QUERY_1 = "SELECT ROW_COUNT FROM cake_portal.gen_id WHERE TABLE_NAME = ?"; String SQL_QUERY_2 = "UPDATE cake_portal.gen_id SET ROW_COUNT = ? WHERE TABLE_NAME = ?"; GenId genId = (GenId) jdbcTemplate.queryForObject(SQL_QUERY_1, new Object[] { tableName }, new BeanPropertyRowMapper(GenId.class)); newId = genId.getRowCount() + 1; jdbcTemplate.update(SQL_QUERY_2, new Object[] { newId, tableName }, new int[] { Types.INTEGER, Types.VARCHAR }); return newId; } public int decrementCakeAndInsideId() throws SQLException { return decrementId("cakes_and_insides"); } public int decrementCakeInsideId() throws SQLException { return decrementId("cake_insides"); } public int decrementCakePriceId() throws SQLException { return decrementId("cake_prices"); } public int decrementCakeTypeId() throws SQLException { return decrementId("cake_types"); } public int decrementCakeId() throws SQLException { return decrementId("cakes"); } public int decrementCakeAndTypeId() throws SQLException { return decrementId("cakes_and_types"); } public int decrementContactTypeId() throws SQLException { return decrementId("contact_types"); } public int decrementContactId() throws SQLException { return decrementId("contacts"); } public int decrementCustomerId() throws SQLException { return decrementId("customers"); } public int decrementDeliveryId() throws SQLException { return decrementId("deliveries"); } public int decrementDiscountId() throws SQLException { return decrementId("discounts"); } public int decrementOrderId() throws SQLException { return decrementId("orders"); } public int decrementPageMetadataId() throws SQLException { return decrementId("page_metadatas"); } public int decrementSitePageId() throws SQLException { return decrementId("site_pages"); } public int decrementUserId() throws SQLException { return decrementId("users"); } private int decrementId(String tableName) { String SQL_QUERY = "UPDATE cake_portal.gen_id SET ROW_COUNT = (ROW_COUNT-1) WHERE TABLE_NAME = ?"; return jdbcTemplate.update(SQL_QUERY, new Object[] { tableName }, new int[] { Types.VARCHAR }); } public void deleteRow(String tableName) { String SQL_QUERY = "DELETE FROM cake_portal.gen_id WHERE TABLE_NAME = ?"; jdbcTemplate.update(SQL_QUERY, new Object[] { tableName }, new int[] { Types.VARCHAR }); } public ArrayList<GenId> getAll(int num) { String SQL_QUERY = "SELECT TABLE_NAME, ROW_COUNT FROM gen_id WHERE ROW_COUNT > ?"; ArrayList<GenId> genIdList = new ArrayList<>(); List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY, new Object[] { num }/*, new BeanPropertyRowMapper(GenId.class)*/); for (Map row : rows) { GenId genId = new GenId(); genId.setTableName((String) row.get("TABLE_NAME")); genId.setRowCount(Integer.parseInt(String.valueOf(row.get("ROW_COUNT")))); genIdList.add(genId); } return genIdList; } }