Solved

What would the code to do this database?

Posted on 2004-05-01
4
232 Views
Last Modified: 2010-03-31
This is the database:

COF_NAME      SUP_ID      PRICE   SALES    TOTAL
--------            ------       -----      -----   -----
Colombian            101      7.99      75      0
French_Roast      49      8.99      10      0
Espresso            150      9.99      60      0


What is the output of this code if i run it against this database.

String query = "SELECT COF_NAME, SALES FROM COFFEES " +  
      "WHERE SALES >=40";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
      String s = rs.getString("COF_NAME");
      int n = rs.getInt("SALES");
      System.out.println(n + " pounds of " + s + " sold this week.");
0
Comment
Question by:aquemini001
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
maheshexp earned 20 total points
ID: 10969926
>String query = "SELECT COF_NAME, SALES FROM COFFEES " +  
     "WHERE SALES >=40";

selects all rows where, the SALES is > 40

>>while (rs.next()) {
>>     String s = rs.getString("COF_NAME");
>>     int n = rs.getInt("SALES");
>>     System.out.println(n + " pounds of " + s + " sold this week.");

moves thru all the rows selected ( if any ), then ges the value of SALES, i.e 75,60 and prints it in the console
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10971265
Be careful to close the while loop ;-)

Your output as per the System.out.println () would be:

75 pounds of Colombian sold this week.
60 pounds of Espresso sold this week.

BTW, you can check the output yourself. What more do you want to ask? If you only want the output of the query explained as per what maheshexp has done, it is more of a database question than a Java question~!

JDBC tutorial - http://java.sun.com/docs/books/tutorial/jdbc/
0
 

Author Comment

by:aquemini001
ID: 10973008
you;re right. sorry. thanks for the help.
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10974100
thanks :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

766 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