Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Core Java Code Examples

Get hashtable keys from value

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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 } } }