Solving JDBC: What to Put in the While Block?

  • Thread starter Darkstar3000
  • Start date
In summary: This will iterate through each row in the Titles table and print out the title, author, and year for each entry.
  • #1
Darkstar3000
29
0
Question about JDBC

Here is a snippet of some JDBC code I wrote, I want to know what I have to put in the while block to iterate the result, I've seen examples of JDBC code but have no idea how you choose what to put in the while block and how it helps with the final result, can anyone explain this to me ?

My code
Code:
ResultSet result = select.executeQuery("SELECT *, FROM Titles");

            System.out.println("Got results: ");/
            
            
            while(result.next()){

               // what do I put here ?
                }

Example Code :

Code:
Statement statement = connection.createStatement();

String sql = "select * from people";

ResultSet result = statement.executeQuery(sql);

while(result.next()) {

    String name = result.getString("name");
    long   age  = result.getLong("age");

    System.out.println(name);
    System.out.println(age);
}

result.close();
statement.close();
 
Last edited:
Physics news on Phys.org
  • #2
Answer: In the example code, the while block is used to iterate through the results of the query. The contents of the while block can be customized depending on what you want to do with the results of the query. In this example, the code is retrieving the name and age from the result set, then printing out the values to the console. In your code, you can use the same approach to retrieve the columns from the result set, then perform an action with that data. For example, if you wanted to print out the contents of the Titles table, you could do something like this: while(result.next()){ String title = result.getString("title"); String author = result.getString("author"); int year = result.getInt("year"); System.out.println("Title: " + title); System.out.println("Author: " + author); System.out.println("Year: " + year + "\n");}
 

1. What is the purpose of the While Block in JDBC?

The While Block in JDBC is used to iterate through the rows of a result set retrieved from a database query.

2. How do I know what to put in the While Block?

The While Block should contain code that performs a specific action for each row in the result set, such as extracting data from the rows and storing it in variables or performing calculations.

3. Can I use any type of loop in the While Block?

Yes, you can use any type of loop in the While Block, as long as it allows you to iterate through the rows of the result set.

4. Do I need to close the result set in the While Block?

Yes, it is important to close the result set after you have finished using it in the While Block. This will release any resources associated with the result set and prevent memory leaks.

5. Can I use multiple While Blocks in a JDBC program?

Yes, you can use multiple While Blocks in a JDBC program, as long as each block is used for a different result set. It is important to remember to close each result set after using it in its respective While Block.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
505
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
997
  • Engineering and Comp Sci Homework Help
Replies
10
Views
10K
Back
Top