Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

You will move the database access code to a separate method which will be called by main. When calling this method you will pass in two strings one contains information about the query to be displayed with the output, the other containing the SQL query to be executed.

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

=+ (b) Define a set function v, on , by (11.6) "o (f, g] = A(g-f).

Answered: 1 week ago

Question

Which form of proof do you find least persuasive? Why?

Answered: 1 week ago