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 com.tsg.techsupportmvc.dao; import com.tsg.techsupportmvc.dto.KnowledgeBaseArticle; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * * @author apprentice */ public class KnowledgeBaseDaoDbImpl implements KnowledgeBaseDao { private static final String SQL_INSERT_ARTICLE = "INSERT INTO `knowledgeBase` (`userId_FK`, `title`, `dateSubmitted`, `articleBody`) values (?, ?, ?, ?)"; private static final String SQL_SELECT_ALL_ARTICLES = "SELECT k.*, u.displayName FROM `knowledgeBase` as k JOIN `users` AS u ON k.userID_FK = u.userId"; private static final String SQL_DELETE_ARTICLE = "DELETE FROM `knowledgeBase` WHERE `articleId` = ?"; private static final String SQL_UPDATE_ARTICLE = "UPDATE `knowledgeBase` SET `userId_FK` = ?, `title` = ?, `dateSubmitted` = ?, `articleBody` = ? WHERE `articleId` = ?"; private static final String SQL_SELECT_ARTICLE = "SELECT k.*, u.displayName FROM `knowledgeBase` as k JOIN `users` AS u ON k.userID_FK = u.userId WHERE `articleId` = ?"; private JdbcTemplate jdbcTemplate; @Autowired public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override @Transactional(propagation = Propagation.REQUIRED, readOnly = false) public KnowledgeBaseArticle addArticle(KnowledgeBaseArticle article) { jdbcTemplate.update(SQL_INSERT_ARTICLE, article.getUserId_FK(), article.getTitle(), article.getSubmittedDate(), article.getArticleBody()); article.setArticleId(jdbcTemplate.queryForObject("SELECT LAST_INSERT_ID()", Integer.class)); return article; } @Override public void removeArticle(int id) { jdbcTemplate.update(SQL_DELETE_ARTICLE, id); } @Override public void updateArticle(KnowledgeBaseArticle article) { jdbcTemplate.update(SQL_UPDATE_ARTICLE, article.getUserId_FK(), article.getTitle(), article.getSubmittedDate(), article.getArticleBody(), article.getArticleId()); } @Override public List<KnowledgeBaseArticle> getAllArticles() { return jdbcTemplate.query(SQL_SELECT_ALL_ARTICLES, new ArticleMapper()); } @Override public KnowledgeBaseArticle getById(int id) { try { return jdbcTemplate.queryForObject(SQL_SELECT_ARTICLE, new ArticleMapper(), id); } catch (EmptyResultDataAccessException e) { return null; } } private static final class ArticleMapper implements RowMapper<KnowledgeBaseArticle> { @Override public KnowledgeBaseArticle mapRow(ResultSet rs, int i) throws SQLException { KnowledgeBaseArticle article = new KnowledgeBaseArticle(); article.setArticleId(rs.getInt("articleId")); article.setTitle(rs.getString("title")); article.setUserId_FK(rs.getString("userId_FK")); article.setDisplayName(rs.getString("displayName")); article.setSubmittedDate(rs.getString("dateSubmitted")); article.setArticleBody(rs.getString("articleBody")); return article; } } }