Question

In this hands-on project, you will create an application that allows the user to connect to...

In this hands-on project, you will create an application that allows the user to connect to a database and query the database.

Write a Java query application that allows the user to connect to the books database and query the books database. Provide the following predefined queries:

  1. Select all authors from the Authors table
  2. Select a specific author and list all books for that author. Include each book’s title, year and ISBN. Display the appropriate data for each query.

Given sql file below:

DROP DATABASE IF EXISTS books;

CREATE DATABASE books;

USE books;

CREATE TABLE Authors

(

AuthorID int NOT NULL AUTO_INCREMENT PRIMARY KEY,

FirstName varchar(30) NOT NULL,

LastName varchar(30) NOT NULL

) ;

CREATE TABLE Titles

(

ISBN varchar(20) NOT NULL PRIMARY KEY,

Title varchar(100) NOT NULL,

EditionNumber int NOT NULL,

Copyright varchar(4) NOT NULL

) ;

CREATE TABLE AuthorISBN

(

AuthorID int NOT NULL,

ISBN varchar(20) NOT NULL,

FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID),

FOREIGN KEY (ISBN) References Titles(ISBN)

) ;

INSERT INTO Authors (FirstName,LastName) VALUES

('Harvey','Deitel') ;

INSERT INTO Authors (FirstName,LastName) VALUES ('Paul','Deitel') ;

INSERT INTO Authors (FirstName,LastName) VALUES ('Andrew','Goldberg') ;

INSERT INTO Authors (FirstName,LastName) VALUES ('David','Choffnes') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0131869000','Visual Basic 2005 How to Program',3,'2006') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0131869000') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0131869000') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0131525239','Visual C# 2005 How to Program',2,'2006') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (1,'0131525239') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES (2,'0131525239') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0132222205','Java How to Program',7,'2007') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(1,'0132222205') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(2,'0132222205') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0131857576','C++ How to Program',5,'2005') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(1,'0131857576') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(2,'0131857576') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0132404168','C How to Program',5,'2007') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(1,'0132404168') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(2,'0132404168') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0131450913','Internet & World Wide Web How to Program',3,'2004') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(1,'0131450913') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(2,'0131450913') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(3,'0131450913') ;

INSERT INTO Titles (ISBN,Title,EditionNumber,Copyright) VALUES

('0131828274','Operating Systems',3,'2004') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(1,'0131828274') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(2,'0131828274') ;

INSERT INTO AuthorISBN (AuthorID,ISBN) VALUES

(4,'0131828274') ;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

question 1:

import java.sql.*;

public class JdbcSelectTest {
public static void main(String[] args) {
try (
  
Connection conn = DriverManager.getConnection();   
Statement stmt = conn.createStatement();
) {
  
String strSelect = "select * from Authors";


ResultSet rset = stmt.executeQuery(strSelect);


System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) {
int AuthorID = rset.getInt("AuthorID");
String FirstName = rset.getString("FirstName");
String LastName = rset.getString("LastName");
System.out.println(AuthorID + ", " + FirstName + ", " + LastName);
++rowCount;
}
System.out.println("Total number of records = " + rowCount);

} catch(SQLException ex) {
ex.printStackTrace();
}
}
}

question 2:

import java.sql.*;

public class JdbcSelectTest {
public static void main(String[] args) {
try (
  
Connection conn = DriverManager.getConnection();   
Statement stmt = conn.createStatement();
) {
  
String strSelect = "select T.ISBN, T.Title, T.editionNumber, T.Copyright from Titles T, AuthorISBN A where T.ISBN=A.ISBN";


ResultSet rset = stmt.executeQuery(strSelect);


System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) {
String ISBN = rset.getString("ISBN");
String Title = rset.getString("Title");
int EditionNumber = rset.getInt("EditionNumber");
String Copyright = rset.getString("Copyright");
System.out.println(ISBN + ", " + Title + ", " + EditionNumber + ", " + Copyright);
++rowCount;
}
System.out.println("Total number of records = " + rowCount);

} catch(SQLException ex) {
ex.printStackTrace();
}
}
}

Add a comment
Know the answer?
Add Answer to:
In this hands-on project, you will create an application that allows the user to connect to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define an SQL view JokesNum that gives the number of jokes each user posts on each...

    Define an SQL view JokesNum that gives the number of jokes each user posts on each day. Database: create table User( userid varchar(30) not null, password varchar(30), firstname varchar(30), lastname varchar(50), email varchar(50), gender char(1), age integer, banned boolean, primary key(userid), unique(email)); create table MyFriends( userid varchar(30), friendid varchar(30), primary key(userid,friendid), foreign key(userid) references User, foreign key(friendid) references User(userid)); Create table Jokes( Jokeid integer, title varchar(100), description varchar(255), authorid varchar(30) not null, primary key(jokeid), posttime Datetime, foreign key(authorid) references User(userid));...

  • Please help me on the SQL queries, thank you so much. Write a query to display the title and publisher as well as the p...

    Please help me on the SQL queries, thank you so much. Write a query to display the title and publisher as well as the publisher contact for each book using JOIN...USING clause. Write a query to show the first and last names of customers who have ordered cooking books. Use the WHERE clause to join the tables. Write a query to show the title, cost and ISBN of each book in the books table. If the book has been ordered,...

  • Write an SQL query to return the users who posted the most number of jokes on...

    Write an SQL query to return the users who posted the most number of jokes on 1/1/2019. Database: create table User( userid varchar(30) not null, password varchar(30), firstname varchar(30), lastname varchar(50), email varchar(50), gender char(1), age integer, banned boolean, primary key(userid), unique(email)); create table MyFriends( userid varchar(30), friendid varchar(30), primary key(userid,friendid), foreign key(userid) references User, foreign key(friendid) references User(userid)); Create table Jokes( Jokeid integer, title varchar(100), description varchar(255), authorid varchar(30) not null, primary key(jokeid), posttime Datetime, foreign key(authorid) references User(userid));...

  • Use the SQL statements provided to create your tables. Write one SQL statement for each of...

    Use the SQL statements provided to create your tables. Write one SQL statement for each of the following problems. You can only use the conditions listed in the tasks. E.g., in task 1, you cannot manually look up pid of Information Systems undergraduate program. Here is the given code: drop table textbook_schedule cascade constraints; drop table textbook_author cascade constraints; drop table schedule cascade constraints; drop table course cascade constraints; drop table textbook cascade constraints; drop table author cascade constraints; drop...

  • Consider the computer science bibliography domain described in Lab Assignment 1. Consider the schema in Figure...

    Consider the computer science bibliography domain described in Lab Assignment 1. Consider the schema in Figure to model this domai Each author in table Authors is identified by a unique authorID which is autogenerated. Each author has a first name, a last name and an email. The email is unique, while the first name and last name may not be unique. Each paper is identified by a unique papeD which is autogenerated. For each paper, we keep in table Papers...

  • Given the following table structure, write the SQL code to display all the first and last...

    Given the following table structure, write the SQL code to display all the first and last names of the individuals that have a last name that begins with the letter 'M! CREATE TABLE Persons Personi int LastName varchar(80). FirstName varchar(80). Address varchar(125). State char(2) Given the following table structure, write the SQL code to display all the first and last names of the individuals that have a last name that does not contain the letter 'S: CREATE TABLE Persons PersonlDint,...

  • a database of employees that corresponds to the employee-payroll hierarchy is provided (see employees.sql to create...

    a database of employees that corresponds to the employee-payroll hierarchy is provided (see employees.sql to create the employees for a MySQL database). Write an application that allows the user to: Add employees to the employee table. Add payroll information to the appropriate table for each new employee. For example, for a salaried employee add the payroll information to the salariedEmployees table 1 is the entity-relationship diagram for the employees database Figure 1: Table relationships in the employees database [1]. Add...

  • Need a PHP code that has a box to type in that allows the user to...

    Need a PHP code that has a box to type in that allows the user to put the information through the PHP site to the database for the following categories: INT, First name, Last name, Sex. Once injected it needs to echo to the page to show the table of all entries. The code below is how far I got with it. MySQL: add a row and query $link = mysql_connect('stewcraw.dotstermysql.com', 'prof', '3632password'); if (!$link) { die('Could not connect: '...

  • Suppose a student is taking IS 620 section 1 and HCC 629 section 1 for spring...

    Suppose a student is taking IS 620 section 1 and HCC 629 section 1 for spring 2019. Use an explicit cursor to print out the titles and prices of textbooks for these two courses. Sample code to create the tables: drop table textbook_schedule cascade constraints; drop table textbook_author cascade constraints; drop table schedule cascade constraints; drop table course cascade constraints; drop table textbook cascade constraints; drop table author cascade constraints; drop table teacher cascade constraints; drop table program cascade constraints;...

  • PHP Programming In this project, you will create a Web page that allows visitors to your...

    PHP Programming In this project, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a database. Create a new document in your text editor and type the <!DOCTYPE> declaration, <html> element, document head, and <body> element. Use the strict DTD and “Guest Book” as the content of the <title> element. Add the following text and elements to the document body: <h2>Enter your name to sign our guest book</h2>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT