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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Get Column Value Using Collection Classes 
 

The HashSet class implements the Set interface which holds the iteration element of the set.

 

Get Column Value Using  Collection Classes

                         

This application illustrates how to retrieve data from mysql table using HashSet class.

The HashSet class implements the Set interface which holds the iteration element of the set. In this example we are using the java.util package to extends this class. There are four type of constructor available in this class:

  • HashSet ():- This HashSet Constructs a new, empty set. 
  • HashSet (Collection<? extends E> c):- This HashSet Constructs a new set containing the elements in the specified collection. 
  • HashSet (int initialCapacity):- Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor.
  • HashSet (int initialCapacity, float loadFactor):- Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

 

The Source Code of Employee.java

 
import java.sql.*;
import java.util.*;

public class Employee{
  private String name;
  private String lastAccess;
  private int id;

  public String getName () {
    return (name);
  }
  public void setName (String name){
    this.name = name;
  }

  public String getLastAccess (){
    return (lastAccess);
  
  public void setLastAccess (String lastAccess){
    this.lastAccess = lastAccess;
  }

  public int getId() {
    return (id);
  }
  public void setId (int id){
    this.id = id;
  }

  public static void main(String[] args) {
    System.out.println("\nGetting All Rows from a table!\n");
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "test";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";
    ArrayList list = new ArrayList();
    Employee emp;
    int count =0;

    try{
      Class.forName(driver).newInstance();
      con = DriverManager.getConnection(url+db, user, pass);
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select *from emp");
      System.out.println("Emp_Id" "\t   " "Emp_name" "\t" "Access_Time");
      
      while (rs.next()) {
        count++;
        emp = new Employee ();
        emp.setId(rs.getInt("id"));
        emp.setName(rs.getString("name"));
        emp.setLastAccess(rs.getString("lastAccess"));
        list.add(emp);
      }
      
      ListIterator li = list.listIterator();
      
      while (li.hasNext()){
        Employee emp1=(Employee)li.next();
        System.out.print(emp1.getId()+"          ");
        System.out.print(emp1.getName()+"\t");
        System.out.print(emp1.getLastAccess());
        System.out.print("\n");
      }    
      rs.close();
      stmt.close();
      con.close();
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

 

Output:

 

Download Source Code

                         

» View all related tutorials
Related Tags: java c string ide array io word vi new key this id element elements for example keyword with to ini

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.