This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch06] 6.7 Exercise #0, Implemented printing the list of projects.
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
- Loading branch information
Showing
5 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} | ||
}*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |