0% found this document useful (0 votes)
104 views

Write A Servlet Program To Select The Record From The Database

The servlet program connects to an Oracle database using JDBC and executes a SQL query to select all records from the Employees table. It retrieves the results and displays them in an HTML table by iterating through the ResultSet and extracting the column values. It closes the database connections and resources. The registration form collects user details through a HTML form and on submit, the servlet uses JDBC to insert the details into an Oracle registration table in the database. It returns a success message if the record is inserted.

Uploaded by

Shobha Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Write A Servlet Program To Select The Record From The Database

The servlet program connects to an Oracle database using JDBC and executes a SQL query to select all records from the Employees table. It retrieves the results and displays them in an HTML table by iterating through the ResultSet and extracting the column values. It closes the database connections and resources. The registration form collects user details through a HTML form and on submit, the servlet uses JDBC to insert the details into an Oracle registration table in the database. It returns a success message if the record is inserted.

Uploaded by

Shobha Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a servlet program to select the record from the database.

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class SelectDB extends HttpServlet


{
     public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
     {
          Statement stmt=null;
          ResultSet rs=null;
          Connection conn= null;
     
          // Set response content type
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          String title = "Database Result";
          String docType =
          "<!doctype html" +
          "transitional//en\">\n";
          out.println(docType +
          "<html>\n" +
          "<head><title>" + title + "</title></head>\n" +
          "<body>");
          try
          {
               // Register JDBC driver
               Class.forName("oracle.jdbc.driver.OracleDriver");

               // Open a connection


               conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "local",
"test");

               // Execute SQL query


               stmt = conn.createStatement();
               String sql;
               sql = "SELECT * FROM Employees";
               rs = stmt.executeQuery(sql);
               out.println("<table border=1 >");
               out.println("<caption><h2>Employee Record</h2></caption>");

               out.println("<tr style='background-color:#ffffb3; color:red'>");


               out.println("<th>Emp Id</th>");
               out.println("<th> Age(Year)</th>");
               out.println("<th>First Name</th>");
               out.println("<th>Last Name</th>");
               out.println("</tr>");
               // Extract data from result set
               while(rs.next())
               {
                    //Retrieve by column name
                    int id  = rs.getInt("id");
                    int age = rs.getInt("age");
                    String first = rs.getString("first");
                    String last = rs.getString("last");

                    //Display values
                    out.println("<tr style='background-color:#b3ffd9;'>");
                    out.println("<td>" + id + "</td>");
                    out.println("<td>" + age + "</td>");
                    out.println("<td>" + first + "</td>");
                    out.println("<td>" + last + "</td>");
                    out.println("</tr>");
               }
               out.println("</table>");
               out.println("</bod;=y></html>");

               // Clean-up environment


               rs.close();
               stmt.close();
               conn.close();
          }
          catch(SQLException se)
          {
               se.printStackTrace();
          }
          catch(Exception e)
          {
               e.printStackTrace();
          }
          finally
          {
               //finally block used to close resources
               try
               {
                    if(stmt!=null)
                         stmt.close();
               }
               catch(SQLException se2)
               {}// nothing we can do
               try
               {
               if(conn!=null)
                    conn.close();
               }
               catch(SQLException se)
               {
               se.printStackTrace();
               }
          }
     }
}

<!doctype html>  
   <body>  
      <form action="servlet/Register" method="post">  
         <fieldset style="width:20%; background-color:#ccffeb">
            <h2 align="center">Registration form</h2><hr>
            <table>
               <tr>
                  <td>Name</td>
                  <td><input type="text" name="userName" required /></td>
               </tr>  
               <tr>
                  <td>Password</td>
                  <td><input type="password" name="userPass" required
/></td>
               </tr>  
               <tr>
                  <td>Email Id</td>
                  <td><input type="text" name="userEmail" required /></td>
               </tr>  
               <tr>
                  <td>Mobile</td>
                  <td><input type="text" name="userMobile" required/></td>
               </tr>  
               <tr>
                  <td>Date of Birth</td>
                  <td><input type="date" name="userDOB" required/></td>
               </tr>  
               <tr>
                  <td>Gender</td>
                  <td><input type="radio" name="gender" value="male"
checked> Male
                  <input type="radio" name="gender" value="female"> Female
</td></tr>
               <tr>
                  <td>Country</td>
                  <td><select name="userCountry" style="width:130px">  
                     <option>Select a country</option>  
                     <option>India</option>  
                     <option>America</option>  
                     <option>England</option>  
                     <option>other</option></select>
                  </td>
               </tr>
               <tr>
                  <td><input type="reset" value="Reset"/></td>
                  <td><input type="submit" value="Register"/></td>
               </tr>
            </table>
         </fieldset>  
      </form>  
   </body>  
</html>

Register.java

import java.io.*;  
import java.sql.*;  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
  
public class Register extends HttpServlet
{  
     public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
     {  
          response.setContentType("text/html");  
          PrintWriter out = response.getWriter();  
          
          String name = request.getParameter("userName");  
          String pwd = request.getParameter("userPass");  
          String email = request.getParameter("userEmail");
          int mobile = Integer.parseInt(request.getParameter("userMobile"));
          String dob = request.getParameter("userDOB");  
          String gender = request.getParameter("gender");  
          String country =request.getParameter("userCountry");  
          
          try
          {  
               //load the driver
               Class.forName("oracle.jdbc.driver.OracleDriver");  
               //create connection object
               Connection
con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","l
ocal","test");  
               // create the prepared statement object
               PreparedStatement ps=con.prepareStatement("insert into
registration values(?,?,?,?,?,?,?)");  
  
               ps.setString(1,name);  
               ps.setString(2,pwd);  
               ps.setString(3,email);  
               ps.setInt(4, mobile);
               ps.setString(5,dob);  
               ps.setString(6,gender);  
               ps.setString(7,country);  
  
               int i = ps.executeUpdate();  
               if(i>0)  
               out.print("You are successfully registered...");  
  
          }
          catch (Exception ex)
          {
               ex.printStackTrace();
          }  
          out.close();  
     }  
}

You might also like