Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Retrieving Data from the table using Statement 
 

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

 

Retrieving Data from the table using Statement

                         

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

To accomplish our goal we first have to make a class named as ServletFetchingData which must extends the abstract HttpServlet class, the name of the class should be such that the other person can understand what this program is going to perform. The logic of the program will be written inside the doGet() method which 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 retrieve the data from 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 firstly need to call the method forName() which is static in nature of the class ClassLoader. 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. As we need to retrieve the data from the table so we need to write a query to select all the records from the table. This query will be passed in the executeQuery() method of Statement object, which returns the ResultSet object. Now the data will be retrieved by using the getString() method of the ResultSet object.

The code of the program is given below:

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

public class ServletFetchingDataFromDatabase1 extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse responsethrows 
                              ServletException, IOException
{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/zulfiqar";
    Connection connection=null;
    try{
      Class.forName("org.gjt.mm.mysql.Driver");
      connection = DriverManager.getConnection(connectionURL, "root""admin");
      Statement st = connection.createStatement();
      ResultSet rs = st.executeQuery("Select * from emp_sal");
      while(rs.next()){
        pw.println("EmpName" "   " "EmpSalary" "<br>");
        pw.println(rs.getString(1"   " + rs.getString(2"<br>");
      }
    }
    catch (Exception e){
      pw.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>ServletFetchingDataFromDatabase</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/ServletFetchingDataFromDatabase</url-pattern>
 </servlet-mapping>
</web-app>

The output of the program is given below: 

Table in the database:

mysql> select * from emp_sal;
+----------+--------+
| EmpName | salary |
+----------+--------+
| zulfiqar | 15000 |
| vinod | 12000 |
+----------+--------+
2 rows in set (0.00 sec)

Download this example:

                         

» View all related tutorials
Related Tags: c http browser class ant process post https methods servlet method sed get type const request constants content value return

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

-i want code fill JTable from database and remove table contents
-in java no grid like flexgrid in VB

Posted by maaz on Tuesday, 02.26.08 @ 13:08pm | #50116

very good
i want code retreive data from table using LIKe ,i'm using NetBeans5.5

Posted by maaz on Thursday, 02.14.08 @ 12:39pm | #48240

how to retrive the data from mysql database

Posted by ramakanth on Friday, 01.4.08 @ 18:23pm | #44513

when i retrieve data from MySql database
through java class file it is not allowing me to use where clause inside the query why?

Posted by alok agarwal on Saturday, 09.29.07 @ 14:42pm | #30473

its very interesting for learning.

Posted by sourabh on Thursday, 03.15.07 @ 15:36pm | #11795

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.