package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
import java.sql.*;

public class SearchAccount_jsp extends HttpJspBase {


  private static java.util.Vector _jspx_includes;

  public java.util.List getIncludes() {
    return _jspx_includes;
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;
    javax.servlet.jsp.PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;


    try {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/html;charset=ISO-8859-1");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n\r\n\r\n");
      out.write("<html>\r\n");
      out.write("<head>");
      out.write("<title>SearchAccount");
      out.write("</title>");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("<h1>Ket qua tra dien thoai theo yeu cau cua ban :");
      out.write("</h1>\r\n");
      out.write("<table border=1 cellPadding=1 cellSpacing=1>\r\n");

        String tentbao = request.getParameter("ttbao");
        String sodthoai = request.getParameter("sodthoai");
        String diachi = request.getParameter("diachi");
        // xay dung lenh SQL
        boolean fsqlstart = true;
        String newSQL = "SELECT * FROM CUSTOMER";
        if (tentbao != null) {
            if (tentbao.length() != 0) {
                newSQL = newSQL + " where tentbao like '%" + tentbao + "%'";
                fsqlstart = false;
            }
        }
        if (sodthoai != null) {
            if (sodthoai.length() != 0) {
                if (fsqlstart) {
                    newSQL = newSQL + " where sodthoai like '%" + sodthoai +
                        "%'";
                }
                else {
                    newSQL = newSQL + " and sodthoai like '%" + sodthoai + "%'";
                }
                fsqlstart = false;
            }
        }
        if (diachi != null) {
            if (diachi.length() != 0) {
                if (fsqlstart) {
                    newSQL = newSQL + " where diachi like '%" + diachi + "%'";
                }
                else {
                    newSQL = newSQL + " and diachi like '%" + diachi + "%'";
                }
                fsqlstart = false;
            }
        }

        String conStr = "jdbc:odbc:DanhBaDT";

        System.out.println("ConStr = " + conStr);

        Statement stmt = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(conStr, "", "");
            stmt = con.createStatement();

            rs = stmt.executeQuery(newSQL);
            System.out.println("NewSQL = " + newSQL);

            int i = 1;
            out.println("<tr><th>So thu tu</th><th>Ten thue bao</th><th>So dien thoai</th><th>Dia chi</th></tr>\n");
            if (rs != null) {
                while (rs.next()) {
                    out.println("<tr><td>" + i++ +"</td><td>" + rs.getString(2) +
                                "</td><td>" + rs.getString(3) + "</td><td>" +
                                rs.getString(4) + "</td></tr>\n");
                }
            }
            out.println("</table>");
//            rs.close();
//            stmt.close(); //con.close();
        }
        catch (Exception e) {
            System.out.println("Error : " + e);
        }
        finally {
            if (rs != null) {
                try {
                    rs.close();
                }
                catch (Exception _ex) {
                    System.out.println("Exception: " + _ex);
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (Exception _ex) {
                    System.out.println("Exception: " + _ex);
                }
            }
            if (con != null) {
                try {
                    con.close();
                }
                catch (Exception _ex) {
                    System.out.println("Exception: " + _ex);
                }
            }

        }

      out.write("\r\n\r\n");
      out.write("<a href='http://localhost:8080/ServletDB/InputForm.jsp'>Quay ve trang chu");
      out.write("</a>\r\n");
      out.write("</body>");
      out.write("</html>\r\n\r\n");
    } catch (Throwable t) {
      out = _jspx_out;
      if (out != null && out.getBufferSize() != 0)
        out.clearBuffer();
      if (pageContext != null) pageContext.handlePageException(t);
    } finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
    }
  }
}
