Solved

Code help for understanding countHashMap

Posted on 2006-10-26
6
162 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Requested array size exceeds VM limit 3 85
Java Loop 6 50
difference of if loops 23 43
Windows 10 IE Certificate Issue 10 42
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

863 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

24 Experts available now in Live!

Get 1:1 Help Now