Write a code for servlet which will display all the fields of Employee table in tabular form.MCS-051,Dec09-3a
import javax.servlet.*;
import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
public class Employee extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/Html");
PrintWriter pw = res.getWriter();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select emp_id,emp_name,emp_dob,mob_no,email from employee");
String tr="";
while(rs.next()){
tr=tr+"<tr>";
tr=tr+"<td>"+rs.getString(1)+"</td>";
tr=tr+"<td>"+rs.getString(2)+"</td>";
tr=tr+"<td>"+rs.getString(3)+"</td>";
tr=tr+"<td>"+rs.getString(4)+"</td>";
tr=tr+"<td>"+rs.getString(5)+"</td>";
tr=tr+"</tr>";
}
String table="<table border='1'>"+tr+"</table>";
pw.write(table);
pw.close();
con.close();
}catch(Exception e)
{
pw.write(""+e);
}
}
}
import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
public class Employee extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/Html");
PrintWriter pw = res.getWriter();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select emp_id,emp_name,emp_dob,mob_no,email from employee");
String tr="";
while(rs.next()){
tr=tr+"<tr>";
tr=tr+"<td>"+rs.getString(1)+"</td>";
tr=tr+"<td>"+rs.getString(2)+"</td>";
tr=tr+"<td>"+rs.getString(3)+"</td>";
tr=tr+"<td>"+rs.getString(4)+"</td>";
tr=tr+"<td>"+rs.getString(5)+"</td>";
tr=tr+"</tr>";
}
String table="<table border='1'>"+tr+"</table>";
pw.write(table);
pw.close();
con.close();
}catch(Exception e)
{
pw.write(""+e);
}
}
}
Comments