Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The instructions are in the pictures. & here is the given code that's needs to be modified I already added in the derby.jar too. :

The instructions are in the pictures. & here is the given code that's needs to be modified I already added in the derby.jar too.
: import java.sql.*;
public class DBTesterApp
{
private static Connection connection = null;
public static void main(String args[])
{
// get the connection and start the Derby engine
connection = MurachDB.getConnection();
if (connection != null)
System.out.println("Derby has been started. ");
// select data from database
printProducts();
printFirstProduct();
printLastProduct();
printProductByCode("java");
// modify data in the database
Product p = new Product("test", "Test Product", 49.50);
insertProduct(p);
printProducts();
deleteProduct(p);
printProducts();
// disconnect from the database
if (MurachDB.disconnect())
System.out.println("Derby has been shut down. ");
}
public static void printProducts()
{
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM Products"))
{
Product p = null;
System.out.println("Product list:");
while(rs.next())
{
String code = rs.getString("ProductCode");
String description = rs.getString("Description");
double price = rs.getDouble("Price");
p = new Product(code, description, price);
printProduct(p);
}
System.out.println();
}
catch(SQLException e)
{
e.printStackTrace(); // for debugging
}
}
public static void printFirstProduct()
{
Product p = null;
String code = rs.getString("ProductCod"); // add code that prints the record for the first product in the Products table
System.out.println("bvbn:");
printProduct(p);
System.out.println();
}
public static void printLastProduct()
{
Product p = null;
// add code that prints the record for the last product in the Products table
System.out.println("Last product:");
printProduct(p);
System.out.println();
}
public static void printProductByCode(String productCode)
{
Product p = null;
// add code that prints the product with the specified code
System.out.println("Product by code: " + productCode);
printProduct(p);
System.out.println();
}
public static void insertProduct(Product p)
{
System.out.println("Insert test: ");
// add code that inserts the specified product into the database
// if a product with the specifed code already exists, display an error message
printProduct(p);
System.out.println();
}
private static void deleteProduct(Product p)
{
System.out.println("Delete test: ");
// add code that deletes the specified product from the database
// if a product with the specified code doesn't exist, display an error message
printProduct(p);
System.out.println();
}
// use this method to print a Product object on a single line
private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(p.getCode(), 8) +
StringUtils.padWithSpaces(p.getDescription(), 44) +
p.getFormattedPrice();
System.out.println(productString);
}
}
image text in transcribed
image text in transcribed
metadata. To do this, you may need to convert SQL data types to Java data types. Exercise 21-1 Work with JDBC In this exercise, you'll write JDBC code that works with the Derby database named MurachDB that was described in the previous chapter. 1. Open the project named ch21_ex1_DBTester that's in the ex_starts directory Add the derby.jar file to the Libraries folder for this project. Review the code in the source files and run the project. It should print all of the records in the Products table to the console three times with some blank product lines in between. 2

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_2

Step: 3

blur-text-image_3

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

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

1. What is your age? 45 or younger 4655 5665 6675 7685 86 or older

Answered: 1 week ago

Question

4x2 1 9X3

Answered: 1 week ago

Question

Explain the causes of indiscipline.

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago