package examples.htmlkona;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import weblogic.db.jdbc.*;
import weblogic.html.*;

/**
 * This simple servlet illustrates how you can write a servlet
 * that not only displays but also modifies database data.
 * 
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class InsertUpdateDelete extends HttpServlet {

  public synchronized void service(HttpServletRequest req, HttpServletResponse res) 
    throws IOException
  {
    Connection conn = null;

    try {
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/html");
      
      defaults.setPageDefaults();
      conn = defaults.login();
      
      Statement stmt = conn.createStatement();
      
      String insert = "insert into emp(empno, ename, job, deptno) values " + 
	              "(8000, 'MURPHY', 'SALESMAN', 10)";
      
      String update = "update emp set ename = 'SMITH', job = 'MANAGER' where empno = 8000";
      
      String delete = "delete from emp where empno = 8000";
      
      stmt.execute(insert);
      
      // Verify results
      TableDataSet ds1 = new TableDataSet(conn, "emp");
      ds1.where("empno = 8000")
	.fetchRecords();
      
      stmt.execute(update);
      
      // Verify results
      TableDataSet ds2 = new TableDataSet(conn, "emp");
      ds2.where("empno = 8000")
	.fetchRecords();
      
      stmt.execute(delete);
      
      // Verify results
      TableDataSet ds3 = new TableDataSet(conn, "emp");
      ds2.where("empno = 8000")
	.fetchRecords();
      
      ServletPage hp = new ServletPage("Insert, Update, Delete");
      hp.getBodyElement()
	.setAttribute(BodyElement.bgColor, HtmlColor.white);
      hp.getBody()
        .addElement(MarkupElement.HorizontalLine)
        .addElement(new HeadingElement("Query results after INSERT", 2))
        .addElement(new HeadingElement("Using SQL:", 3))
        .addElement(new LiteralElement(insert))
        .addElement(new LiteralElement(ds1))
        .addElement(MarkupElement.HorizontalLine)
        .addElement(new HeadingElement("Query results after UPDATE", 2))
        .addElement(new HeadingElement("Using SQL:", 3))
        .addElement(new LiteralElement(update))
        .addElement(new LiteralElement(ds2))
        .addElement(MarkupElement.HorizontalLine)
        .addElement(new HeadingElement("Query results after DELETE", 2))
        .addElement(new HeadingElement("Using SQL:", 3))
        .addElement(new LiteralElement(delete))
        .addElement(new LiteralElement(ds3))
        .addElement(MarkupElement.HorizontalLine)
	.addElement("Copyright 1996-2000 by BEA Systems, Inc. All Rights Reserved.");

      hp.output(res.getOutputStream());

      ds1.close();
      ds2.close();
      ds3.close();

    }
    catch (Exception e) {
      defaults.showException(e, res.getOutputStream());
    }

    // Always close the connection in a finally block.
    finally {
      try {conn.close();} catch (Exception e2) {;}
    }
  }
}

