Write a servlet which retrieves the data from database?



package ddrs;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class RetrieveDataBaseServ extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) {
  res.setContentType("text/html");
  PrintWriter pw = res.getWriter();
  try {
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
   Connection con = DriverManager.getConnection("oracle:jdbc:thin:@localhost:1521: Hanuman " , "scott " , "tiger ");
    Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from emp");
    while (rs.next()) {
     pw.println(rs.getString(1) + " " + rs.getString(2));
    }
   } catch (Exception e) {
    res.sendError(504, "PROBLEM IN SERVLET...");
   }
  }
 }


web.xml:

<web-app>
<servlet>
<servlet-name>Babu</servlet-name>
<serclet-class>ddrs.RetrieveDataBaseServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Babu</servlet-name>
<url-pattern>Dinesh</url-pattern>
</servlet-mapping>
</web-app>


No comments:

Post a Comment