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 All Rows from a Database Table 
 

Here, you will learn how to retrieve all rows from a database table. You know that table contains the data in rows and columns format.

 

Retrieving All Rows from a Database Table

                         

Here, you will learn how to retrieve all rows from a database table. You know that table contains the data in rows and columns format. If you want to access the data from a table then you need to use some APIs and methods. See brief descriptions for retrieving all rows from a database table as below:

Description of program:

Program establishes the connection between MySQL database and java file so that the we can retrieve all data from a specific database table. If any exception occurs then shows a message "SQL code does not execute.".

Description of code:

executeQuery(String sql):
This method executes the SQL statement and returns a single ResultSet object. It takes string type parameter for executing the SQL statement.

SELECT * FROM table_name:
Above code retrieves all data from specific database table.

getInt(String column_name):
This method is of  java.sql.ResultSet interface that takes string type parameter and returns an integer type values.

getString(String column_name):
This method is same as getInt() method but it returns the string type values.

Here is the code of program:

import java.sql.*;

public class GetAllRows{
  public static void main(String[] args) {
    System.out.println("Getting All Rows from a table!");
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";
    try{
      Class.forName(driver).newInstance();
      con = DriverManager.getConnection(url+db, user, pass);
      try{
        Statement st = con.createStatement();
        ResultSet res = st.executeQuery("SELECT * FROM  employee6");
        System.out.println("Emp_code: " "\t" "Emp_name: ");
        while (res.next()) {
          int i = res.getInt("Emp_code");
          String s = res.getString("Emp_name");
          System.out.println(i + "\t\t" + s);
        }
        con.close();
      }
      catch (SQLException s){
        System.out.println("SQL code does not execute.");
      }    
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Output of program:

C:\vinod\jdbc\jdbc-mysql>java GetAllRows
Getting All Rows from a table!
Emp_code:           Emp_name:
10                        vinod
11                        Amar
15                        Aman
1               
           sushil

Download this example.

                         

» View all related tutorials
Related Tags: c database com search table ant data time import io help get column vi port new name int this searching

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

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

this will do all my work you r maintaining site really well congrats

Posted by satish on Thursday, 11.13.08 @ 20:22pm | #81655

i'm working with image i.e FaxViewer using JApplet and i want to get all the image in table but i want to see one image at a time, when i press next button then next row image will see on the JApplet. So this is my problem will you plz solve this problem

Posted by prasad on Monday, 12.17.07 @ 14:38pm | #42703

Hi this is a great effort . these are too much helping articles specially for beginners.

Posted by irfan ullah on Monday, 07.2.07 @ 12:26pm | #20574

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.