d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question With A Java Code
Add Reply New Topic New Poll
Member
Posts: 27,871
Joined: Jun 30 2008
Gold: 266,852.03
Sep 28 2013 09:11pm
I've got a question related to Java coding. I'm quite new to coding, it only has been 3 classes i've had up to now and no lie, I find it quite hard. For one of my code, I'm supposed to get this result:

My current code gives me this:

I'm missing the part where each date can have more than a single Customer_ID + quantity. I was wondering how to execute that in Java coding?

To give you an idea, here's a part of my coding (in which I believe something is missing).

Code

query = "SELECT"
+ " SALES_ORDER_DATE,"
+ " OUTPUT_DESCRIPTION,"
+ " CUSTOMER_ID,"
+ " SUM(QUANTITY) AS QUANTITY"
+ " FROM SALES_ORDER"
+ " INNER JOIN SALES_ORDER_LINE_ITEM"
+ " ON SALES_ORDER.SALES_ORDER_ID = SALES_ORDER_LINE_ITEM.SALES_ORDER_ID"
+ " INNER JOIN BOM"
+ " ON SALES_ORDER_LINE_ITEM.PRODUCT_ID = BOM.OUTPUT_PRODUCT_ID"
+ " AND SALES_ORDER.SALES_ORDER_DATE = BOM.EFFECTIVE_DATE"
+ " GROUP BY"
+ " SALES_ORDER_DATE,"
+ " OUTPUT_DESCRIPTION,"
+ " CUSTOMER_ID"
+ " ORDER BY SALES_ORDER_DATE";
result = stmt.executeQuery(query);


int dateIndex = 0;
Hashtable<String, Integer> quantities = new Hashtable<String, Integer>();


while (result.next() && dateIndex < dates.size()) {
String date = result.getString("SALES_ORDER_DATE");
String client = result.getString("CUSTOMER_ID");

while (dates.elementAt(dateIndex).compareTo(date) < 0) {
out.print("<tr><td>" + dates.elementAt(dateIndex) + "</td>");
for (String iÉtiquette : étiquettes) {
out.print("<td align='right'>");
if (quantities.get(iÉtiquette) != null) {
out.print(client + " : " + quantities.get(iÉtiquette));
quantities.remove(iÉtiquette);
} else {
out.print("&nbsp;");
}
out.print("</td>");
}
out.print("</tr>");
dateIndex++;
}

String produit = result.getString("OUTPUT_DESCRIPTION");
int quantity = result.getInt("QUANTITY");

quantities.put(produit, quantity);

}

while (dateIndex < dates.size() && quantities.size() != 0) {
out.print("<tr><td>" + dates.elementAt(dateIndex) + "</td>");
for (String iÉtiquette : étiquettes) {
out.print("<td align='right'>");
if (quantities.get(iÉtiquette) != null) {
out.print(quantities.get(iÉtiquette));
quantities.remove(iÉtiquette);
} else {
out.print("&nbsp;");
}
out.print("</td>");
}
out.print("</tr>");
dateIndex++;
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 28 2013 09:21pm
if you're iffy about java, i recommend using more methods, where each method does primarily one task. this will make it easier for you to find problems in your logic. as you add multiple conditions / loops, you'll end up confusing yourself.

with that said, i'm not clear what your current problem is. i assume the problem is that you're only getting the first entry per cell and the rest aren't there? or are those entries ending up on the wrong line? i'd just tweak your sql query so that a single row in your presentation is a single row in your dataset, as opposed to having many rows in your dataset and trying to combine them into a single row in your presentation. then you simply display the data without messing with logic. then this becomes a simple sql question as opposed to a java question.

so your java code would look something to this effect:
Code

ResultSet rs = stmt.executeQuery(...);
while (rs.next()){
processRow(rs);
}


....

void processRow(rs){
// make it very straight forward to write the column data
StringBuilder html = new StringBuilder("<tr>");
html.append("<td>");
html.append(rs.getString(...));
html.append("</td>");
html.append("</tr>");
String output = html.toString();
log.trace(output);
out.print(output);
}


This post was edited by carteblanche on Sep 28 2013 09:29pm
Member
Posts: 27,871
Joined: Jun 30 2008
Gold: 266,852.03
Sep 28 2013 09:29pm
Quote (carteblanche @ Sep 29 2013 03:21am)
if you're iffy about java, i recommend using more methods, where each method does primarily one task. this will make it easier for you to find problems in your logic. as you add multiple conditions / loops, you'll end up confusing yourself.

with that said, i'm not clear what your current problem is. i assume the problem is that you're only getting the first entry per cell and the rest aren't there? or are those entries ending up on the wrong line? i'd just tweak your sql query so that a single row in your presentation is a single row in your dataset, as opposed to having many rows in your dataset and trying to combine them into a single row in your presentation. then you simply display the data without messing with logic. then this becomes a simple sql question as opposed to a java question.


Yes, my problem is only getting the first entry per cell (not exactly the first, it seems like it randomly takes one of the entry?). Though, that's the result I need to reproduce exactly to get my points, which is why i can't have them all on different rows.

This post was edited by goldfinger978 on Sep 28 2013 09:29pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 28 2013 09:30pm
Quote (goldfinger978 @ Sep 28 2013 11:29pm)
Yes, my problem is only getting the first entry per cell. Though, that's the result I need to reproduce exactly to get my points, which is why i can't have them all on different rows.


ok, so this is a sql problem not a java problem. change your sql query
Member
Posts: 27,871
Joined: Jun 30 2008
Gold: 266,852.03
Sep 28 2013 09:37pm
Quote (carteblanche @ Sep 29 2013 03:30am)
ok, so this is a sql problem not a java problem. change your sql query


Here was the problem I had to resolve (which makes me believe the query is fine since it was given). I'm pretty sure it's the java code part that is missing a line somewhere, though I do not know where/what it would be:
Quote


Vous avez accès aux données de la compagnie MUESLI. Produisez un tableau HTML indiquant pour chaque label de produits (champ OUTPUT_DESCRIPTION du BOM) la liste des clients ayant acheté le produit et la quantité qu'ils ont achetée à chaque date. Utilisez une colonne par produit et une ligne par date. Utilisez les requêtes suivantes :

SELECT DISTINCT OUTPUT_DESCRIPTION FROM BOM

SELECT SIM_DATE.SIM_DATE FROM SIM_DATE

SELECT
    SALES_ORDER_DATE,
    OUTPUT_DESCRIPTION,
    CUSTOMER_ID,
    SUM (QUANTITY) AS QUANTITY
FROM SALES_ORDER
    INNER JOIN SALES_ORDER_LINE_ITEM
        ON SALES_ORDER.SALES_ORDER_ID = SALES_ORDER_LINE_ITEM.SALES_ORDER_ID
    INNER JOIN BOM
        ON SALES_ORDER_LINE_ITEM.PRODUCT_ID = BOM.OUTPUT_PRODUCT_ID
        AND SALES_ORDER.SALES_ORDER_DATE = BOM.EFFECTIVE_DATE
GROUP BY
    SALES_ORDER_DATE,
    OUTPUT_DESCRIPTION,
    CUSTOMER_ID
ORDER BY SALES_ORDER_DATE


This post was edited by goldfinger978 on Sep 28 2013 09:37pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 28 2013 09:52pm
well my plan was to move the problem away from java and to sql since you're not that comfortable with java. but if you gotta do it in java, then like i said earlier, refactor it. introduce more methods.

how about you change your hashtable to be <string, list> where the single etiquette is associated with ALL customer / quantities as opposed to a single one. then you can have a helper method loop through your result set once and build your hash table. then you can use another helper method that transforms the list into a single string for your table cell. then your rendering will be much simpler like the sample i have above.
Member
Posts: 27,871
Joined: Jun 30 2008
Gold: 266,852.03
Sep 28 2013 10:33pm
Quote (carteblanche @ Sep 29 2013 03:52am)
well my plan was to move the problem away from java and to sql since you're not that comfortable with java. but if you gotta do it in java, then like i said earlier, refactor it. introduce more methods.

how about you change your hashtable to be <string, list> where the single etiquette is associated with ALL customer / quantities as opposed to a single one. then you can have a helper method loop through your result set once and build your hash table. then you can use another helper method that transforms the list into a single string  for your table cell. then your rendering will be much simpler like the sample i have above.


I'll give that a try, thanks
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll