Question 2: consider the following library relational database schema Write a program segment to retrieves the...

Free

70.2K

Verified Solution

Question

Programming

Question 2: consider the following library relational databaseschema Write a program segment to retrieves the list of books thatbecame overdue yesterday and that prints the book title andborrower name for each.

1- Use JDBC with Java as the host language

Answer & Explanation Solved by verified expert
4.1 Ratings (603 Votes)

Answer:

Java Code:

package com.java2novice.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyResultSetEx //Main Class
{

    public static void main(String a[]) //Main Function
{
         
        try
   {
            Class.forName(\"oracle.jdbc.driver.OracleDriver\");
            Connection con = DriverManager.getConnection(\"jdbc:oracle:thin:@::\",\"user\",\"password\"); // Provide the database name,username and password details to establish the connection with database
            Statement stmt = con.createStatement();
            System.out.println(\"Created DB Connection....\"); //Displaying if connection is established with database
            ResultSet rs = stmt.executeQuery(\"SELECT BK.Title, BW.Name FROM BOOK_LOANS BL JOIN BORROWER BW ON BL.Card_no=BW.Card_no JOIN BOOK BK ON BL.Book_id=BK.Book_id WHERE BL.Due_date < SYSDATE - 1;\"); //SQL query to fetch book title and borrower name for whom due date was yesterday
            while(rs.next())
   {
                System.out.println(rs.getString(1)); //Printing the book title
                System.out.println(rs.getString(2)); //Printing the borrower name
            }
            rs.close();
            con.close(); //Closing the connection with database
        }
  catch (ClassNotFoundException e)
  {
            System.err.print(\"SQLException: \");
   e.printStackTrace();
        } catch (SQLException e)
  {
            System.err.print(\"SQLException: \");
   e.printStackTrace();
        }
    }
}


Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students