Insert teacher record in Database - Java

Q. Write a java program to accept the details of Teacher (TId, Name, Address) from the user and insert it into the Database.

Answer:

In below example we create teacher registration page in html which accepts id, name and address. After clicking submit button, all data gets stored in the database.

Create a table in a database to store the record.

Create table teacherdetails (tid number, name varchar2(20), address varchar2(40);

register.html

<!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>TId</td>
                              <td><input type="text" name="TId" required /></td>
                         </tr>  
                         <tr>
                              <td>Name</td>
                              <td><input type="text" name="Name" required /></td>
                         </tr>  
                         <tr>
                              <td>Address</td>
                              <td><textarea name="address" placeholder="Enter address here..."></textarea></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();            
          int id = Integer.parseInt(request.getParameter("TId"));
          String name = request.getParameter("name");  
          String address = request.getParameter("address");  
          try
          {  
               //load the driver
               Class.forName("oracle.jdbc.driver.OracleDriver");  
               //create connection object
               Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","local","test");  
               // create the prepared statement object
               PreparedStatement ps=con.prepareStatement("insert into TeacherDetails values(?,?,?)");  
  
               ps.setInt(1, id);
               ps.setString(2,name);  
               ps.setString(3,address);  
  
               int i = ps.executeUpdate();  
               if(i>0)  
               out.print("You are successfully registered...");  
          }
          catch (Exception ex)
          {
               ex.printStackTrace();
          }  
          out.close();  
     }  
}


web.xml

<web-app>   
     <servlet>  
          <servlet-name>Register</servlet-name>  
          <servlet-class>Register</servlet-class>  
     </servlet>    
     <servlet-mapping>  
          <servlet-name>Register</servlet-name>  
          <url-pattern>/servlet/Register</url-pattern>  
     </servlet-mapping>   
     <welcome-file-list>  
          <welcome-file>register.html</welcome-file>  
     </welcome-file-list>   
</web-app>


Output:

teacher registration

teacher registration