Solved

Code help for understanding countHashMap

Posted on 2006-10-26
6
164 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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