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″)

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”;
   Statement stmtnew = conNew.createStatement();
   ResultSet rsNew = stmtnew.executeQuery(newsql);
   int i=1;
    String itemNo=rsNew.getString(“item_no”);
    String matNo=rsNew.getString(“mat_no”);
    String itMat= itemNo+matNo;
    System.out.println(“Inside Hashmap:for itMat”+ itMat+”Hashmap:value is:”+hmData.get(itMat));
   catch(Exception e){}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s