*[java][アプリ]改!掲示板

20090329011526

今日は前回の宿題をかたしました。
改め掲示板をリファクタしました。
結構自信作〜♪

だからいっぱい指摘していただきたい。
挑戦こそ生きがいですけん!!!

明日M山さんとK本さんにみていただこ〜っと。

ほなぁ行きます。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>project</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>bbs</servlet-name>
  	<servlet-class>bbs.Entry</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>bbs</servlet-name>
  	<url-pattern>/bbs</url-pattern>
  </servlet-mapping>
</web-app>
<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
 <%@page import="bbs.*" %>
 <%@page import="java.util.*" %>
 <% 
 List list = (List)request.getAttribute("rec");
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>掲示板</title>
</head>
<body>
<h1 align="center">掲示板</h1>
<form method="post" action="Entry">
	<table align="center">
		<tr>
			<td>名前</td>
			<td><input type="text" name="name" maxlength="10" size="40"></td>
		</tr>
		<tr>
			<td>題名</td>
			<td><input type="text" name="title" maxlength="50" size="40"></td>
		</tr>
		<tr>
			<td>本文</td>
			<td><textarea name="text" cols="40" rows="10"></textarea></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" value="送信"></td>
		</tr>
	</table>
</form>
<% for(int i = 0; i< list.size(); i++){
	Dto dto = (Dto)list.get(i);
	
%>
<hr>
<p>投稿者:<%=dto.getName() %></p>
<p>タイトル:<%=dto.getTitle() %></p>
<p>本文<br><%=dto.getText() %></p>
<%
}
%>
</body>
</html>
package bbs;

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


public class Dao {
	
	private Connection con; 
	private DBManager dbManager;

	public Dao() {
		try {
			dbManager = new DBManager();
			con = dbManager.getConnection();
		} catch (Exception e) {
			System.out.println("Connection取得に失敗・・・");
			e.printStackTrace();
		}
	}
	
	public List<Dto> find() throws Exception {
		return find("select * from bbs");
	}
	
	private List<Dto> find(String sql) throws Exception {
		List<Dto> list = new ArrayList<Dto>();
		System.out.println(con);
		Statement stm = con.createStatement();
		ResultSet rs = stm.executeQuery(sql);
		while(rs.next()){
			Dto dto = new Dto();
			dto.setId(rs.getInt("id"));
			dto.setName(rs.getString("name"));
			dto.setTitle(rs.getString("title"));
			dto.setText(rs.getString("text"));
			list.add(dto);
		}
		if(stm != null){
			stm.close();
		}
		return list;
	}
	
	public void update(Dto dto) throws Exception {
		update("insert into bbs(name,title,text)values('" + 
				dto.getName() + "','" +
				dto.getTitle() + "','" +
				dto.getText() + "');");
	}
	private void update(String sql) throws Exception {
		Statement stm = con.createStatement();
		stm.executeUpdate(sql);
	}
}
package bbs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBManager {

	private Connection con;
	
	private final static String DRIVER = "org.gjt.mm.mysql.Driver";
	
	private final static String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=SJIS";
	
	private final static String USER = "root";
	
	private final static String PASSWD = "mysql";
	
	public Connection getConnection() throws Exception {
		Class.forName(DRIVER);
			return con == null? con = DriverManager.getConnection(URL,USER,PASSWD) : con;
	}
	
	public void closeConnection() throws SQLException {
		if (con != null) {
			con.close();
		}
	}
}
package bbs;
import java.io.Serializable;


public class Dto implements Serializable{
	
	private int id;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	private String name;
	
	private String title;
	
	private String text;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}
package bbs;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Entry extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("#----Entry doGet ----");
		Dao dao = new Dao();
		try {
			List list = dao.find();
			req.setAttribute("rec", list);
		} catch (Exception e) {
			System.out.println("データ取得に失敗");
			e.printStackTrace();
		}
		getServletConfig().getServletContext().getRequestDispatcher("/bbs.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("#----Entry doPost ----");
		Dto dto = new Dto();
		dto.setName(req.getParameter("name"));
		dto.setTitle(req.getParameter("title"));
		dto.setText(req.getParameter("text"));
		
		Dao dao = new Dao();
		try {
			dao.update(dto);
		} catch (Exception e1) {
			System.out.println("Insert失敗");
			e1.printStackTrace();
		}
		try {
			List list = dao.find();
			req.setAttribute("rec", list);
		} catch (Exception e) {
			System.out.println("データ取得に失敗");
			e.printStackTrace();
		}
		getServletConfig().getServletContext().getRequestDispatcher("/bbs.jsp").forward(req, resp);
	}

}

実はたくさん問題もあって自覚症状はあります。
・Connectionをcloseしてない
jstlとEL式を使えばもっとシンプルにかける
・PreparedStatementを使用したらDAOのInsert処理がきれいにいくはず・・・・
等等・・・・

あ〜先輩のコードむっちゃ写経したいっす。
明日見せてもらって写経しよっと。

今回の作成手順は・・・・
・Daoを実装(damePGはDaoからのほうが作りやすい)
・Testクラスを作ってDaoが正常に実行できるか調べる。
・Entryを作成しJspを表示できるか調べる。
・あとはちょろちょろっとロジックを付け足す。

以上!!
実はもうすぐ友達の結婚式です。
「PGやってる」というと
この業界でない方は「PCやったらなんでもできるやろ??」という感覚で注文してくる。

引き受けたのは「生い立ちビデオ」・・・・
ってもろGUIっすやんと思いながら、ふと思いつきました。

結婚式に来た人来てない人を管理するシステムを次作ってみよう。
というわけでいつか作ります。


ラクル〜♪(←まだゆうてんのかいって話で・・・)