package danhba;

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

public class SearchAccount extends HttpServlet {
	private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
	//Initialize global variables
	public void init() throws ServletException {
	}
	//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 bgcolor=\"#ffffff\">");

		String strTentb = request.getParameter("tentb");

		// Xay dung cau SELECT
		String newSQL = "SELECT * FROM CUSTOMERS ";
		if (strTentb != null && strTentb.length() > 0) {
			newSQL += " WHERE Tentb Like '%" + strTentb + "%'";
		}

		// Tao ket noi den Database
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			String conStr = "jdbc:odbc:DanhBaDB";

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

			// Tao mot Statement
			stmt = con.createStatement();
			rs = stmt.executeQuery( newSQL );
			out.println("<pre>");
			if (rs != null  ) {
				int i = 0;
				for (i = 0 ; rs.next() ; i++ ) {

					String name = rs.getString("Tentb");
					String sodt = rs.getString("Sodt");
					out.println(name + " - " + sodt + " - " + rs.getString("Diachi"));
				}
				rs.close();

				out.println("Tim thay so ket qua la: " + i);

			}else {
				out.println("Khong tim thay");
			}

			out.println("</pre>");
			con.close();
		}catch(Exception ex) {
			System.out.println("Exception: " + ex );
		}




		out.println("</body></html>");
	}
	//Clean up resources
	public void destroy() {
	}
}