Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

. You do not have the tables, but what is required is the code. You might not see the output. Show how to retrieve a data in your code based on the starter code and the output given. If you use printf to display each query you will run into an interesting (quirky?) problem – printf assumes that every % indicates some type of formatting. Our first query ends with “…LIKE 'D%'”. If you try to print this using printf you will get an error. The “best” solution to this is to use print or println instead of printf – neither print nor println use the % as a formatting command. A starter code is given below:

Starter code

//DisplayAuthors.java
//Displaying the contents of the Authors table.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class DisplayAuthors {

   public static void main(String[] args) {
          
       final String DATABASE_URL = "jdbc:derby:books";
       final String SELECT_QUERY =
              
               "SELECT firstName, lastName FROM Authors";
              
                          
      
       // use try-with-resources to connect to and query the database
       try (
               Connection connection = DriverManager.getConnection(
                       DATABASE_URL, "deitel", "deitel");
               Statement statement = connection.createStatement();
               ResultSet resultSet = statement.executeQuery(SELECT_QUERY))
          
              
       {
          
       // get ResultSet's meta data  
       ResultSetMetaData metaData = resultSet.getMetaData();
       int numberOfColumns = metaData.getColumnCount();
      
       System.out.printf("Authors Table of Books Database:%n%n");  
  
       // display the names of the columns in the ResultSet
       for (int i = 1; i <= numberOfColumns; i++)
           System.out.printf("%-8s\t", metaData.getColumnName(i));
       System.out.println();
      
       // display query results
       while (resultSet.next())
       {
           for (int i = 1; i <= numberOfColumns; i++)
               System.out.printf("%-8s\t", resultSet.getObject(i));
           System.out.println();
       }
   }// AutoCloseable objects' close methods are called now
  
   catch (SQLException sqlException)
   {
       sqlException.printStackTrace();
   }

}


}


Sample Output

Figure 24.13: SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE 'D%'

AUTHORID         FIRSTNAME LASTNAME

1              Paul    Deitel

⁞                ⁞                       ⁞

Figure 24.15: SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName ASC

AUTHORID         FIRSTNAME LASTNAME

3              Abbey              Deitel

⁞                ⁞                       ⁞

Figure 24.16: SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName DESC

AUTHORID         FIRSTNAME LASTNAME

4              Dan     Quirk

⁞                ⁞                       ⁞

Figure 24.19: SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName

FIRSTNAME       LASTNAME    ISBN

Abbey      Deitel 0132121360

⁞                ⁞                       ⁞

Figure 24.23: SELECT AuthorID, FirstName, LastName FROM Authors

AUTHORID         FIRSTNAME LASTNAME

1              Paul    Deitel

⁞                ⁞                       ⁞

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.