Solved

Code help for understanding countHashMap

Posted on 2006-10-26
6
165 Views
Last Modified: 2010-03-31
Hi
I have 20 categories of product, Each category contains more than 100 products. I want to create a java class that will query the database and get  the total number of products under each category, Like this:

Spiderman Category = Total products under this category 65

I can see that "countHashMap" will do the trick, but I am not sure how to use it. I know the question can be confusing so pls ask me if you need more information. Thank you in advance to anyone who jumps in:)
0
Comment
Question by:atwork2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17814054
select count(*) from product_table where category = 65

?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17814071
Then you can do

map.put(65, resultSet.getInt(1));

(assuming >= Java 1.5)
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 200 total points
ID: 17814209
> get the total number of products under each category

Assume you have two tables: category_table and product_table
category_table columns: id (primary key) | name | ...
product_table columns: category_id (foreign key) | ...

SELECT category_name,COUNT(category_id)
FROM category_table,product_table
where id = category_id
GROUP BY category_name
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:atwork2003
ID: 17814263
Thank you for quick and great response. I am trying these out, will update soon to get more insight:)
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 300 total points
ID: 17814270
create a function to return a Map object:

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
      "jdbc:oracle:thin:@xyz.abcdef.com:1521:yourSID","username","password");

String sql = "select categoryName, count(*) as Total from yourTableName group by categoryName order by categoryName";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();

Map mp = new HashMap();
while (rs.next()) {
      mp.put(rs.getString("categoryName"), rs.getString("Total"));
}
rs.close();
stmt.close();
conn.close();
retunr mp;
0
 

Author Comment

by:atwork2003
ID: 17906518
Thanks a lot guys:) Sorry forgot to close the question, apologize for the inconvenience.

Thank you
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question