先輩コードを写経してみた

今日は先輩のコードを写経してみた。
前に作った掲示板改のコード先輩バージョン。
無理やり僕のjspにあわせたんでちょっと(←かなり)
先輩のコードからかけ離れたけど、UPします。

package M_bbs;

import java.io.IOException;
import java.sql.Connection;
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 BbsServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Connection con = ConnectionUtil.getConnection();
		List<Bbs> bbsData = Bbs.findAll(con);
		req.setAttribute("bbsData", bbsData);
		ConnectionUtil.close(con);
		
		req.getRequestDispatcher("/bbs.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		validateParameter(req);
		
		Connection con = ConnectionUtil.getConnection();
		Bbs bbs = new Bbs(con);
		bbs.setName(req.getParameter("name"));
		bbs.setTitle(req.getParameter("title"));
		bbs.setText(req.getParameter("text"));
		bbs.save();
		
		ConnectionUtil.commit(con);
		ConnectionUtil.close(con);
		
		doGet(req, resp);
	}

	private void validateParameter(HttpServletRequest req) {
	}
	
}
package M_bbs;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import java.sql.PreparedStatement;

public class Bbs implements Serializable {

	private Connection con;
	private int id;
	private String name;
	private String title;
	private String text;
	
	public Bbs(Connection con) {
		this.con = con;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	public static List<Bbs> findAll(Connection con) {
		try {
			ResultSet rs = con.createStatement().executeQuery("select * from bbs order by 1 desc;");
			List<Bbs> result = new ArrayList<Bbs>();
			while (rs.next()) {
				Bbs bbs = new Bbs(con);
				bbs.setId(rs.getInt("id"));
				bbs.setName(rs.getString("name"));
				bbs.setTitle(rs.getString("title"));
				bbs.setText(rs.getString("text"));
				result.add(bbs);
			}
			return result;
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}
	
	public void save() {
		try {
			PreparedStatement ps = con.prepareStatement("insert into bbs(name,title,text) values(?, ?, ?);");
			ps.setString(1, this.name);
			ps.setString(2, this.title);
			ps.setString(3, this.text);
			ps.execute();
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}
	
}
package M_bbs;

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

public class ConnectionUtil {

	public static Connection getConnection() {
		return createConnection();
	}

	private static Connection createConnection() {
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "mysql");
			con.setAutoCommit(false);
			return con;
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}
	public static void commit(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			throw new IllegalStateException(e);
		}
	}
	public static void close(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			throw new IllegalStateException(e);
		}
	}
}

前の課題やったConnectioncloseのタイミング、PrepareStatementの使い方がかなり見えた。
次のやつで早速実装してみる。