Friday 1 July 2011

Get hashtable keys from value


Get hashtable keys from value

class keyhash
    {
     ResultSet rs;
     Hashtable hash;
     Vector v;
     static int i;
     Enumeration e;
    
    public static void main(String[] args)
        {
         keyhash gs = new keyhash();
         System.out.println("Hello World!");
         gs.getresultset();
    }
  
    public void getresultset()
        {
         Hashtable hash = new Hashtable();
         Vector v = new Vector();
         boolean connected = dbConnect();
        // Establish database connection here
        
         if(connected == true)
             {
                 try{
                 PreparedStatement getArticles = db.con.prepareStatement
                 ("select * from xx where id = ?"
                // sample query
                
                 )
                
                
                 String artid;
                 getArticles.setInt(1, 4);
                //This passes integer 4 as a parameter in
                //the sql query
                
                
                 rs = getArticles.executeQuery();
                
                 while (rs.next() == true)
                
                     {
                     key = rs.getString(1);
                    
                     value = rs.getString(2);
                    
                     hash.put(key,value);
                    //Hashtable populated
                    
                 }
                
                 fillvector(hash,v);
                 getkeys(v);
                 rs.close();
             }
                 catch(Exception ex){
                
                 ex.printStackTrace();
             }
         }
        
     }
    public void fillvector(Hashtable hash,Vector v)
         {
         int j=0;
         boolean success;
         Enumeration e = hash.keys();
         while(e.hasMoreElements())
             {
             String key = (String)(e.nextElement());
             String value = (String)hash.get(key);
            
            
             if(value.matches("*****"))
             //Put the value here to retrieve the
            // corresponding keys
                 {
                 v.addElement(key);
                //Add the corresponding keys to the vector
             }
         }
        
     }
    public void getkeys(Vector v)
         {
         Enumeration ev= v.elements();
         while (ev.hasMoreElements())
             {
             System.out.println(ev.nextElement());
            // Print the keys related to a single value
         }
      
     }
  
}

People who read this post also read :



No comments:

Post a Comment