import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JoinTableServlet extends HttpServlet
{
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws IOException, ServletException
		{
		res.setContentType("text/html");
		PrintWriter pw = res.getWriter();
		Connection con;
		PreparedStatement ps;
		ResultSet rs;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://192.168.10.13/data","root","root");
			ps = con.prepareStatement("SELECT * FROM Employee NATURAL JOIN Department");
			rs = ps.executeQuery();
			pw.println("<table><tr>");
			pw.println("<td width = '30%'>EmpId</td>");
			pw.println("<td width = '30%'>Name</td>");
			pw.println("<td width = '30%'>DepId</td>");
			pw.println("<td>Salary</td>");
			pw.println("</tr>");
			pw.println("</table>");
			while(rs.next())
			{
				Integer eId = rs.getInt("Id");
				String name = rs.getString("Name");
				Integer dId = rs.getInt("DepartmentId");
				Integer salary = rs.getInt("Salary");
				pw.println("<table><tr>");
				pw.println("<td width = '30%'>"+eId+"</td>");
				pw.println("<td width = '30%'>"+name+"</td>");
				pw.println("<td width = '30%'>"+dId+"</td>");
				pw.println("<td >"+salary+"</td>");
				pw.println("</tr>");
				pw.println("</table>");
			}
			try
			{
				con.close();
				ps.close();
			}
			catch(Exception e)
			{
				pw.println(e);
			}
		}
		catch(SQLException sx)
		{
			pw.println(sx);
		}
		catch(ClassNotFoundException cx)
		{
			pw.println(cx);
		}
		}
}
