Solved

Code help for understanding countHashMap

Posted on 2006-10-26
6
169 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

622 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