Servlets - SPLessons

Servlet Jdbc ResultSet

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Jdbc ResultSet

Servlet Jdbc ResultSet

shape Description

A ResultSet object keeps up a cursor that focuses to the present row in the result set. The expression "result set" alludes to the row, column information contained in a ResultSet object. The following are the some categorized methods of ResultSet interface.
  • Get methods: used to see the information in the sections of the present line being pointed by the cursor.
  • Update methods: used to refresh the information in the segments of the present line.
  • Navigational methods: used to move the cursors.
Example on Servlet Jdbc ResultSet with ResultMetaData: index.html [html] <html> <form action="./welcome"> Enter your ID:<input type="text" name="Id"/> <input type="submit" value="Click here to retrieve data"> </form> </html> [/html] web.xml [xml] <web-app> <servlet> <servlet-name>ServletJdbcResultSet</servlet-name> <servlet-class>ServletJdbcResultSet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletJdbcResultSet</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app> [/xml] ServletJdbcResultSet.java [java] import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletJdbcResultSet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection con = null; String Id = request.getParameter("Id"); int ID = Integer.parseInt(Id); try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "system"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee where id =" + ID); out.print("<table width=50% border=1 bgcolor='ffff'>"); out.print("<caption>Result:</caption>"); /* Printing column names */ ResultSetMetaData rsmd = rs.getMetaData(); int total = rsmd.getColumnCount(); out.print("<tr>"); for (int i = 1; i <= total; i++) { out.print("<th>" + rsmd.getColumnName(i) + "</th>"); } out.print("</tr>"); /* Printing result */ while (rs.next()) { out.print("<tr> < td > "+rs.getInt(1)+" < /td> < td > "+rs.getString(2)+" < /td> < td > "+rs.getString(3)+" < /td> < td > "+rs.getDate(4)+" < /td> < td > "+rs.getDate(5)+" < /td> < td > "+rs.getString(6)+" < /td> < td > "+rs.getString(7)+" < /td> < /tr>"); } out.print("</table>"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } out.close(); } } } [/java] The functionality of printStackTrace is very simple, but very useful tool for diagnosing an Exception. It tells developer what happened and where in the code this happened. Output Following is the page where the user has to enter an Id ,make sure to Id should be created. When enter an Id result will be as follows.

Summary

shape Key Points

  • The public boolean absolute() method is utilized to move the cursor to the specified row.
  • The public boolean relative() is utilized to move the cursor to the specified row where it may be positive or negative.