Solved

Code help for understanding countHashMap

Posted on 2006-10-26
6
161 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
Comment Utility
select count(*) from product_table where category = 65

?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
> 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:atwork2003
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot guys:) Sorry forgot to close the question, apologize for the inconvenience.

Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 66
Securing Jmx Console and web console 2 45
Groovy:unable to resolve class error 2 27
mockito example issue 8 26
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…
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 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…
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:

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now