package servletdb;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class SearchAccount    extends HttpServlet {
    static final private String CONTENT_TYPE = "text/html";
    private Connection con;
    //Initialize global variables
    public void init() throws ServletException {
    }

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>SearchAccount</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a GET. This is the reply.</p>");
        out.println("</body></html>");
    }

    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>SearchAccount</title></head>");
        out.println("<body>");
        out.println("<h1>Ket qua tra dien thoai theo yeu cau cua ban :</h1>");
        out.println("<table border=1 cellPadding=1 cellSpacing=1>");
        String tentbao = request.getParameter("tenthuebao");
        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 url = System.getProperty("user.dir") + "\\danhbadthoai.mdb";
        //String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + url + "";
        String conStr = "jdbc:odbc:DanhBaDT";
        //System.out.println("URL = " + url);
        System.out.println("ConStr = " + conStr);

        Statement stmt = null;
        ResultSet rs = null;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(conStr, "", "");
            stmt = con.createStatement();
//                                     ResultSet.TYPE_SCROLL_INSENSITIVE,
//                                     ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery(newSQL);
            System.out.println("NewSQL = " + newSQL);
// stmt.getResultSet(); // rs will be scrollable, will not show changes made by others,
// and will be updatable
            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);
                }
            }

        }
        out.println(
            "<a href='http://localhost:8080/ServletDB/inputform'>Quay ve trang chu</a>");
        out.println("</body></html>");
    }

    //Clean up resources
    public void destroy() {
    }
}
