
main.java file
import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
// Connect to the library_db
// 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")
);
}
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

- For every block chain, what information is always registered within every block? (a)The previous block is hash, (b)The current block is hash, (c)The next block is hash, (d) all the previous block is hash?arrow_forwardThere are typically two uses for database logs. Learn more about them here:arrow_forwardIN PHP What did we need to store in the database for 2-factor authentication to be possible?arrow_forward
- For what reasons do business blockchains often use database-style interfaces?arrow_forward1. Load packages to work with Database Link.2. Open connection with SQL database named "publisher".3. Find the directory on your computer where the \ is locatedpublisher database In mathematica wolframarrow_forwardExplain the concept of distributed hash tables (DHTs) and their use in distributed systems like peer-to-peer (P2P) networks.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





