Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch06] 6.7 Exercise #0, Implemented printing the list of projects.
Browse files Browse the repository at this point in the history
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
  • Loading branch information
dhsim86 committed Apr 15, 2017
1 parent f1c8738 commit 52af366
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 0 deletions.
196 changes: 196 additions & 0 deletions src/main/java/Lesson06/MySqlProjectDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package Lesson06;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

@Component("projectDao")
public class MySqlProjectDao implements ProjectDao {

DataSource dataSource;

@Override
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public List<Project> selectList() throws Exception {

Connection connection = null;
Statement stmt = null;
ResultSet res = null;

try {

connection = dataSource.getConnection();

stmt = connection.createStatement();
res = stmt.executeQuery(
"select pno, pname, content, sta_date, end_date, state, cre_date, " +
"end_date, state, cre_date, tags " +
"from projects " +
"order by pno desc"
);

List<Project> projectList = new ArrayList<>();

while(res.next()) {

projectList.add(new Project()
.setNo(res.getInt("pno"))
.setTitle(res.getString("pname"))
.setContent(res.getString("content"))
.setStartDate(res.getDate("sta_date"))
.setEndDate(res.getDate("end_date"))
.setState(res.getInt("state"))
.setCreatedDate(res.getDate("cre_date"))
.setTags(res.getString("tags"))
);
}

return projectList;
}
catch (Exception e) {
throw e;
}
finally {
try { if (res != null) res.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}

try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}

@Override
public int insert(Project project) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;

try {

connection = dataSource.getConnection();
stmt = connection.prepareStatement(
"insert into projects(pname, content, sta_date, end_date, state, cre_date, tags)" +
" values(?, ?, ?, ?, ?, now()"
);

stmt.setString(1, project.getTitle());
stmt.setString(2, project.getContent());
stmt.setDate(3, java.sql.Date.valueOf(project.getStartDate().toString()));
stmt.setDate(4, java.sql.Date.valueOf(project.getEndDate().toString()));
stmt.setInt(5, project.getState());

int result = stmt.executeUpdate();

return result;
}
catch (Exception e) {
throw e;
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}

@Override
public int delete(int no) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;

try {

connection = dataSource.getConnection();
stmt = connection.prepareStatement(
"delete from projects" +
" where pno = ?"
);

stmt.setInt(1, no);

int result = stmt.executeUpdate();
return result;
}
catch (Exception e) {
throw e;
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}

@Override
public Project selectOne(int no) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;
ResultSet res = null;

try {

connection = dataSource.getConnection();
stmt = connection.prepareStatement(
"select pno, pname, content, sta_date, end_date, state, cre_date, " +
"end_date, state, cre_date, tags " +
"from projects " +
" where = ?"
);
stmt.setInt(1, no);

res = stmt.executeQuery();
res.next();

Project project = new Project()
.setNo(res.getInt("pno"))
.setTitle(res.getString("pname"))
.setContent(res.getString("content"))
.setStartDate(res.getDate("sta_date"))
.setEndDate(res.getDate("end_date"))
.setState(res.getInt("state"))
.setCreatedDate(res.getDate("cre_date"))
.setTags(res.getString("tags"));

return project;
}
catch (Exception e) {
throw e;
}
finally {
try { if (res != null) res.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}

try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}
/*
@Override
public int update(Project project) throws Exception {
Connection connection = null;
PreparedStatement stmt = null;
try {
connection = dataSource.getConnection();
stmt = connection.prepareStatement(
"update projects "
);
}
catch (Exception e) {
throw e;
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}*/
}
73 changes: 73 additions & 0 deletions src/main/java/Lesson06/Project.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Lesson06;

import java.util.Date;

public class Project {

protected int no;
protected String title;
protected String content;
protected Date startDate;
protected Date endDate;
protected int state;
protected Date createdDate;
protected String tags;

public int getNo() {
return no;
}
public Project setNo(int no) {
this.no = no;
return this;
}
public String getTitle() {
return title;
}
public Project setTitle(String title) {
this.title = title;
return this;
}
public String getContent() {
return content;
}
public Project setContent(String content) {
this.content = content;
return this;
}
public Date getStartDate() {
return startDate;
}
public Project setStartDate(Date startDate) {
this.startDate = startDate;
return this;
}
public Date getEndDate() {
return endDate;
}
public Project setEndDate(Date endDate) {
this.endDate = endDate;
return this;
}
public int getState() {
return state;
}
public Project setState(int state) {
this.state = state;
return this;
}
public Date getCreatedDate() {
return createdDate;
}
public Project setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
return this;
}
public String getTags() {
return tags;
}
public Project setTags(String tags) {
this.tags = tags;
return this;
}

}
16 changes: 16 additions & 0 deletions src/main/java/Lesson06/ProjectDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Lesson06;

import java.util.List;

import javax.sql.DataSource;

public interface ProjectDao {

public void setDataSource(DataSource dataSource);

List<Project> selectList() throws Exception;
public int insert(Project project) throws Exception;
public int delete(int no) throws Exception;
public Project selectOne(int no) throws Exception;
//public int update(Project project) throws Exception;
}
21 changes: 21 additions & 0 deletions src/main/java/Lesson06/ProjectListController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Lesson06;

import java.util.Map;

@Component("/project/list.do")
public class ProjectListController implements Controller {

ProjectDao projectDao;

public ProjectListController setProjectDao(ProjectDao projectDao) {
this.projectDao = projectDao;
return this;
}

@Override
public String execute(Map<String, Object> model) throws Exception {

model.put("projectList", projectDao.selectList());
return "/Lesson06/ProjectList.jsp";
}
}
47 changes: 47 additions & 0 deletions src/main/webapp/Lesson06/ProjectList.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="Lesson06.Project" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>

<!DOCTYPE html>
<html>

<head>
<title>Project List</title>
</head>

<body>
<jsp:include page="../Lesson05/Header.jsp"/>
<h1> Project List </h1>

<p><a href='add.do'> New Project </a>

<table border="1">
<tr>
<th> Number </th>
<th> Title </th>
<th> Start Date </th>
<th> End Date </th>
<th> Status </th>
<th> </th>
</tr>

<c:forEach var="project" items="${projectList}">
<tr>
<td> ${project.no} </td>
<td> <a href="update.do?no=${project.no}">${project.title}</a> </td>
<td> ${project.startDate} </td>
<td> ${project.endDate} </td>
<td> ${project.state} </td>
<td> <a href="delete.do?no=${project.no}">[Delete]</a>
</tr>
</c:forEach>

</table>

<jsp:include page="../Lesson05/Tail.jsp"/>

</body>

</html>

0 comments on commit 52af366

Please sign in to comment.