0% found this document useful (0 votes)
33 views2 pages

Print 3

This servlet retrieves data from a database and displays it in an HTML table. It connects to the database using JDBC and ODBC, executes a SQL query to select all records from the studentdetail table, and iterates through the result set to output the name and id fields into an HTML table which is sent in the response.

Uploaded by

Praveen Pal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

Print 3

This servlet retrieves data from a database and displays it in an HTML table. It connects to the database using JDBC and ODBC, executes a SQL query to select all records from the studentdetail table, and iterates through the result set to output the name and id fields into an HTML table which is sent in the response.

Uploaded by

Praveen Pal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Servlet for retieving data From a database:

package p1;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.sql.*;

public class RetrieveData extends HttpServlet {

public void doGet (HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:dsn");

Statement st = con.createStatement();

ResultSet rs = st.executeQuery("select * from studentdetail");

out.println("<html><body><table><tr><td>Name</td><td>Id</td></tr>");

while (rs.next()) {

out.print("<tr><td>" + rs.getString(1) + "</td>");

out.println("<td>" + rs.getString(2) + "</td></tr>");

out.print("</table></body></html>");

}
} catch (Exception e) {

e.printStackTrace();

} finally {

out.close();

Output:

You might also like