Home Servlets Inserting Data In Database table using Statement

Related Tutorials


 
 

Share on Google+Share on Google+

Inserting Data In Database table using Statement

Advertisement
In this program we are going to insert the data in the database from our java program in the table stored in the database.

Inserting Data In Database table using Statement

     

In this program we are going to insert the data in the database from our java program in the table stored in the database.

To accomplish our goal we first have to make a class named as ServletInsertingData, which must extends the abstract HttpServlet class, the name of the class should be such that other person can understand what this program is going to perform. The logic of the program will be written inside the doGet() method that takes two arguments, first is HttpServletRequest interface and the second one is the HttpServletResponse interface and this method can throw ServletException.

Inside this method call the getWriter() method of the PrintWriter class. We can insert the data in the database only and only if there is a connectivity between our database and the java program. To establish the connection between our database and the java program we first need to call the method forName(), which is static in nature of the class Class. It takes one argument which tells about the database driver we are going to use. Now use the static method getConnection() of the DriverManager class. This method takes three arguments and returns the Connection object. SQL statements are executed and  results are returned within the context of a connection. Now your connection has been established. Now use the method createStatement() of the Connection object which will return the Statement object. This object is used for executing a static SQL statement and obtaining the results produced by it. We have to insert a values into the table so we need to write a query for inserting the values into the table. This query we will write inside the executeUpdate() method of the Statement object. This method returns int value.

If the record will get inserted in the table then output will show "record has been inserted"  otherwise "sorry! Failure".

The code of the program is given below: 

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

public class DataInsertion extends HttpServlet{
  public void doGet(HttpServletRequest request, 
  HttpServletResponse response
)throws 
  ServletException, IOException
{  
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  String url = 
   "jdbc:mysql://localhost/zulfiqar?user=root&password=admin"
;
  Connection conn;
  ResultSet rs;
  try{
  Class.forName("org.gjt.mm.mysql.Driver");
  conn = DriverManager.getConnection(url);
  Statement statement = conn.createStatement();
  String query = "insert into emp_sal values('zulfiqar', 15000)";
  int i = statement.executeUpdate(query);
  if(i!=0){
  out.println("The record has been inserted");
  }
  else{
  out.println("Sorry! Failure");
  }
  rs = statement.executeQuery("select * from emp_sal");
  while(rs.next()){
  out.println("<p><table>" + rs.getString(1" " 
 
+ rs.getInt(2"</p></table>");
  }
  rs.close();
  statement.close();
  }
  catch (Exception e){
  System.out.println(e);
  }
  }
}

XML File for this program

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd"> -->

<web-app>
 <servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>DataInsertion</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/DataInsertion</url-pattern>
 </servlet-mapping>
</web-app>

Table in the database before Insertion:

mysql> select * from emp_sal;
Empty set (0.02 sec)

The output of the program is given below:

Table in the database after Insertion:

mysql> select * from emp_sal;
+----------+--------+
| EmpName | salary |
+----------+--------+
| zulfiqar | 15000 |
+----------+--------+
1 row in set (0.02 sec)

Download this example

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 17, 2007

Related Tutorials

Discuss: Inserting Data In Database table using Statement   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
akshay
March 8, 2013
regarding this program

when i am running this program then it is showing the error java.lang.ClassNotFoundException: com.gjt.mm.mysql.Driver
harsha
December 7, 2011
appreciation

this is the best tutorial i have come across..keep it up:)
akshay jagtap
March 8, 2013
regarding this program

when i am running this program then it is showing the error java.lang.ClassNotFoundException: com.gjt.mm.mysql.Driver
akshay jagtap
March 8, 2013
error

i m getting error java.lang.classNotFoundException: org.jdbc.mysql.Driver can u find wat is error?
DrGreen
March 23, 2013
Problem

I tried this servlet, but I did some modifications to it: I changed url = "jdbc:mysql://localhost:3060/zulfiqar?user=root&password=toor"; and Class.forName("com.mysql.jdbc.Driver"); It compiles, builds and deploys corretly, but when I run it, a blan webpage appears and nothing happens. What should I do ? I am using a DB made in Navicat (similar to MySQL) on localhost and a mysql connector driver in netbeans along with a glassfish server
abhy
July 3, 2013
Result

java.lang.ClassNotFoundException: com.gjt.mm.mysql.Driver How to resolve this error..
DMCA.com