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.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.activation.DataSource; import static konditer_reorganized_database.Main.genIdDao; import konditer_reorganized_database.bean.Cake; 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 public class CakeDao implements CakeDaoInterface { 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; } public CakeDao() { } /* @Override public String getCakeName(int cakeId) { String SQL_QUERY = "SELECT name_cake FROM galary WHERE id = ?"; String cakeName = null; String result = ""; List<Map<String, Object>> rows = jdbcTemplate.queryForList(SQL_QUERY, new Object[]{cakeId}); for (Map<String, Object> row : rows) { cakeName = (String) row.get("name_cake"); result = result + " : " + cakeName + "\n"; } return result; //return "??"; } */ @Override public void addCake(int cakeId, int customerId, double cakePrice, int orderId, String cakeName, double cakeWeight, String cakePhoto, String cakeKeywords, boolean cakeStatus) throws DataAccessException { String SQL_QUERY = "INSERT INTO cakes ( CAKE_ID, " + "CUSTOMER_ID, " + "CAKE_PRICE, " + "ORDER_ID, " + "CAKE_NAME, " + "CAKE_WEIGHT, " + "CAKE_PHOTO_MINI, " + "CAKE_PHOTO_MAXI, " + "CAKE_KEYWORDS, " + "CAKE_STATUS ) " + "VALUES (?,?,?,?,?,?,?,?,?,?)"; //int cakeId1 = genIdDao.getCakeId(); /* ??? ??? 2- ? ?? ? */ //try{ int rowCount = jdbcTemplate.update(SQL_QUERY, new Object[] { cakeId, customerId, cakePrice, orderId, cakeName, cakeWeight, "" + cakeId + "_s.jpg", "" + cakeId + ".jpg", cakeKeywords, cakeStatus }, new int[] { Types.INTEGER, Types.INTEGER, Types.DOUBLE, Types.INTEGER, Types.VARCHAR, Types.DOUBLE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN }); Logger.getLogger(CakeDao.class.getName()).log(Level.SEVERE, " : {0} .", rowCount); //}catch(DataAccessException e){ // Logger.getLogger(CakeDao.class.getName()).log(Level.SEVERE, ". ."); //genIdDao.decrementCakeId(); //} } @Override public Cake getCake(int cakeId) { String SQL_QUERY = "SELECT CAKE_ID, CUSTOMER_ID, CAKE_PRICE, ORDER_ID, " + "CAKE_NAME, CAKE_WEIGHT, CAKE_PHOTO_MINI, CAKE_PHOTO_MAXI, " + "CAKE_KEYWORDS, CAKE_STATUS, TIMESTAMP " + "FROM cakes " + "WHERE CAKE_ID = ?"; Cake cake = (Cake) jdbcTemplate.queryForObject(SQL_QUERY, new Object[] { cakeId }, new BeanPropertyRowMapper(Cake.class)); return cake; } @Override public ArrayList<Cake> getAllCakes() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public ArrayList<Cake> getAllCakes(String searchValue) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public ArrayList<Cake> getAllCakesByCustomerId(int customerId) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public ArrayList<Cake> getAllCakesByOrderId(int orderId) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public ArrayList<Cake> searchCakes(String sqlQuery) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int updateCake(int cakeId, int newCustomerId, double newCakePrice, int newOrderId, String newCakeName, double newCakeWeight, String newCakePhoto, String newCakeKeywords, boolean newCakeStatus) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int deleteCake(int cakeId) { String SQL_QUERY = "DELETE FROM cakes WHERE CAKE_ID = ? "; int rowCount = 0; rowCount = jdbcTemplate.update(SQL_QUERY, new Object[] { cakeId }, new int[] { Types.INTEGER }); Logger.getLogger(CakeDao.class.getName()).log(Level.SEVERE, " . {0} .", rowCount); /* if (rowCount > 0){ try { genIdDao.decrementCakeId(); } catch (SQLException ex) { Logger.getLogger(CakeDao.class.getName()).log(Level.SEVERE, null, ex); } } */ return rowCount; } }