import java.io.*;
import java.sql.*;
import javax.servlet.http.*;

public class DBUpdate extends HttpServlet {
  @Override  
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    doPost(req, res);
  }

  @Override    
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {

    Connection conn = null;

    res.setContentType("text/html; charset=Windows-31J");
    PrintWriter out = res.getWriter();
    
    out.println("<html><head></head><body>");

    String id =     req.getParameter("id");
    String street = req.getParameter("street");
    String city   = req.getParameter("city");
    
    try {   
      String user = "sa", password="";
      
      Class.forName("org.hsqldb.jdbcDriver");         
      String url = "jdbc:hsqldb:hsql://localhost";
      conn = DriverManager.getConnection(url, user, password);
      Statement stmt = conn.createStatement();
      stmt.executeUpdate("UPDATE customer SET street = '" + street +"', city ='"+ city 
                                +"' WHERE id = "+ id);

    } catch (ClassNotFoundException e) {
      out.println("クラスが見つかりません。");
    } catch (SQLException e) {
      out.println("テーブルの更新に失敗しました。");
    } finally {
      try {
        if (conn != null) { conn.close(); }
      } catch (SQLException e) {}
    }
    out.println("テーブルを更新しました。");
    out.println("<table border='true'>");   
    out.println("<tr><th>id</th><th>street</th><th>city</th></tr>");
    out.println("<tr><td>"+ id +"</td><td>"+ street + "</td><td>" + city + "</td></tr>");
    out.println("</table>");
    out.println("</body></html>");
    out.close();
  }
}
