main.java file import java.sql.*; public class Main {       public static void main(String[] args) {         try {             // Connect to the library_db database, via the jdbc:mysql: channel on localhost (this PC)             // Use username "otheruser", password "swordfish".             Connection connection = DriverManager.getConnection(                     "jdbc:mysql://localhost:3306/library_db?useSSL=false",                     "otheruser",                     "swordfish"                     );             // Create a direct line to the database for running our queries             Statement statement = connection.createStatement();             ResultSet results;             int rowsAffected;             // Set up finished, do some stuff:                          // executeQuery: runs a SELECT statement and returns the results.             results = statement.executeQuery("SELECT title, qty FROM books");             // Loop over the results, printing them all.             while (results.next()) {                 System.out.println(results.getString("title") + ", " +results.getInt("qty"));             }               // Add a new book:             rowsAffected = statement.executeUpdate(                     "INSERT INTO books VALUES (3001, 'Programming 101', 'Jane Doe', 1)"                 );             System.out.println("Query complete, " + rowsAffected + " rows added.");             printAllFromTable(statement);                          // Change a book:             rowsAffected = statement.executeUpdate(                     "UPDATE books SET qty=500 WHERE id=1001"                 );             System.out.println("Query complete, " + rowsAffected + " rows updated.");             printAllFromTable(statement);                          // Clear a book:             rowsAffected = statement.executeUpdate(                     "DELETE FROM books WHERE id=3001"                 );             System.out.println("Query complete, " + rowsAffected + " rows removed.");             printAllFromTable(statement);                          // Close up our connections             results.close();             statement.close();             connection.close();                      } catch (SQLException e) {             // We only want to catch a SQLException - anything else is off-limits for now.             e.printStackTrace();         }     }          /**      * Method printing all values in all rows.      * Takes a statement to try to avoid spreading DB access too far.      *       * @param a statement on an existing connection      * @throws SQLException      */     public static void printAllFromTable(Statement statement) throws SQLException{                  ResultSet results = statement.executeQuery("SELECT id, title, author, qty FROM books");         while (results.next()) {             System.out.println(                     results.getInt("id") + ", "                     + results.getString("title") + ", "                     + results.getString("author") + ", "                             + results.getInt("qty")                 );         }     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

main.java file

import java.sql.*;
public class Main {
 
    public static void main(String[] args) {
        try {
            // Connect to the library_db database, via the jdbc:mysql: channel on localhost (this PC)
            // Use username "otheruser", password "swordfish".
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/library_db?useSSL=false",
                    "otheruser",
                    "swordfish"
                    );
            // Create a direct line to the database for running our queries
            Statement statement = connection.createStatement();
            ResultSet results;
            int rowsAffected;
            // Set up finished, do some stuff:
            
            // executeQuery: runs a SELECT statement and returns the results.
            results = statement.executeQuery("SELECT title, qty FROM books");
            // Loop over the results, printing them all.
            while (results.next()) {
                System.out.println(results.getString("title") + ", " +results.getInt("qty"));
            }
 
            // Add a new book:
            rowsAffected = statement.executeUpdate(
                    "INSERT INTO books VALUES (3001, 'Programming 101', 'Jane Doe', 1)"
                );
            System.out.println("Query complete, " + rowsAffected + " rows added.");
            printAllFromTable(statement);
            
            // Change a book:
            rowsAffected = statement.executeUpdate(
                    "UPDATE books SET qty=500 WHERE id=1001"
                );
            System.out.println("Query complete, " + rowsAffected + " rows updated.");
            printAllFromTable(statement);
            
            // Clear a book:
            rowsAffected = statement.executeUpdate(
                    "DELETE FROM books WHERE id=3001"
                );
            System.out.println("Query complete, " + rowsAffected + " rows removed.");
            printAllFromTable(statement);
            
            // Close up our connections
            results.close();
            statement.close();
            connection.close();
            
        } catch (SQLException e) {
            // We only want to catch a SQLException - anything else is off-limits for now.
            e.printStackTrace();
        }
    }
    
    /**
     * Method printing all values in all rows.
     * Takes a statement to try to avoid spreading DB access too far.
     * 
     * @param a statement on an existing connection
     * @throws SQLException
     */
    public static void printAllFromTable(Statement statement) throws SQLException{
        
        ResultSet results = statement.executeQuery("SELECT id, title, author, qty FROM books");
        while (results.next()) {
            System.out.println(
                    results.getInt("id") + ", "
                    + results.getString("title") + ", "
                    + results.getString("author") + ", "        
                    + results.getInt("qty")
                );
        }
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education