Servlets - SPLessons

Servlet JDBC Insertion

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

Servlet JDBC Insertion

Servlet JDBC Insertion

shape Description

Before inserting or deleting or updating the data in data base programmer need to remebmer the following steps.
  • Importing The Packages
  • Register The JDBC Driver
  • Open A Connection
  • Execute A Query
  • Clean Up The Environment
The following is an example for the Servlet JDBC Insertion on Statement Interface The Statement interface gives strategies to execute queries with the database. it gives factory method to get an object of ResultSet. index.html [java] <html> <HEAD> <TITLE>A Servlet</TITLE> </HEAD> <body> <form action="./welcome" method="get"> <table border="1" bgcolor="ffff"> <tr> <td>ID</td> <td>firstname</td> <td>lastname</td> <td>Start_date(YYYYMMDD)</td> <td>End_date(YYYYMMDD)</td> <td>Salary</td> <td>city</td> </tr> <tr> <td><input type="text" name="Id"/></td> <td><input type="text" name="firstName"/></td> <td><input type="text" name="lastName"/></td> <td><input type="text" name="Start_date"/></td> <td><input type="text" name="end_date"/></td> <td><input type="text" name="salary"/></td> <td><input type="text" name="city"/></td> </tr> </table> <input type="submit" value="submit"/> </form> </body> </html> [/java] Here created a text boxes to enter the data.  web.xml [java] <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>ServletInsertion</servlet-name> <servlet-class>ServletInsertion</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletInsertion</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app> [/java] Make sure that servlet name should be same. ServletJdbcInsert.java [java] import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; 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 ServletInsertion 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); String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String start_date = request.getParameter("Start_date"); String end_date = request.getParameter("end_date"); String salary = request.getParameter("salary"); String city = request.getParameter("city"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system"); Statement statement = con.createStatement(); int result = statement.executeUpdate("insert into employee values('"+Id+"',"+"'"+firstName+"',"+"'"+lastName+"',to_date('"+start_date+"','yyyymmdd'),to_date('"+end_date+"','yyyymmdd'),'"+salary+"','"+city+"')"); out.println(result +" row sccessfully inserted your data"); out.flush(); out.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } [/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. The Servlet Request is an interface which defines different methods to handle the client requests to access a servlet, ServletRequest provides an instance to give the requests of client for the servlet, where servlet container establish a object and send an argument to the service method. Output: Program should be compiled from HTML page, where enter the values and click submit. When click on submit rows will be effected.

Summary

shape Key Points

  • The getConnection() is the method of DriverManager class is utilized to build a connection with the database
  • The createStatement() is used to create a statement.
  • In database queries will be executed by using executeQuery().