<%@page import="java.sql.*" %>

<%
        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("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.println(
            "<a href='http://localhost:8080/ServletDB/inputform'>Quay ve trang chu</a>");
        out.println("</body></html>");
%>
