Java Basic Problems

Java: Database to HashMap

Sometimes we may need to collect data or information from database to HashMap in java. Here I have given an example to show how to collect data from database and then  use it as key of HashMap.
HashMap, you may know that, takes two values, first one is KEY Value ( must be Unique) and the second one is Actual Value. Using that key value in HashMap you may search that actual value you entered.
For example:
if ( hmData.get(itMat1)==”1″)
     
     {
      row.add(“Yes”);
     }

In the given example   hmData.put(itMat,”1″);   itMat is the  key value  and regarding this KeyValue I have entered “1” as the actual value to  use in my software.

Here is the sample code to collect or retrieve data from database to HashMap:

HashMap hmData=new HashMap();
   Connection conNew =  PuConnection.DB_Connection();
   String newsql = “select distinct item_no,mat_no from a_nr_allocate_temp”;
   try
   {
   Statement stmtnew = conNew.createStatement();
   ResultSet rsNew = stmtnew.executeQuery(newsql);
   int i=1;
   while(rsNew.next()){
    String itemNo=rsNew.getString(“item_no”);
    String matNo=rsNew.getString(“mat_no”);
    String itMat= itemNo+matNo;
    
    hmData.put(itMat,”1″);
    System.out.println(“Inside Hashmap:for itMat”+ itMat+”Hashmap:value is:”+hmData.get(itMat));
    
    }
   rsNew.close();
   stmtnew.close();
   conNew.close();
   }
   catch(Exception e){}