/notes/code/java/GuestProgramSolution.java (7267222eedb3b3f7399837136e54b02a9328e007) (3805 bytes) (mode 100644) (type blob)
// code/java/GuestProgramSolution.java
import java.sql.*;
import java.util.Scanner; // Importing a java API to read from the keyboard.
/*
* This first part is "standard". Just note that we allow multiple statements and that
* the ResultsSet we will construct with our conn objects will be scrollables.
*/
public class GuestProgram_Solution {
public static void main(String[] args) {
try (Connection conn =
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/?user=testuser&password=password&allowMultiQueries=true");
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ) {
stmt.execute(
"DROP SCHEMA IF EXISTS HW_GUEST_PROGRAM;"
+ "CREATE SCHEMA HW_GUEST_PROGRAM;"
+ "USE HW_GUEST_PROGRAM;"
+ "CREATE TABLE GUEST("
+ "Id INT PRIMARY KEY,"
+ "Name VARCHAR(30),"
+ "Confirmed BOOL"
+ ");"
+ "CREATE TABLE BLACKLIST("
+ "Name VARCHAR(30)"
+ ");"
+ "INSERT INTO BLACKLIST VALUES (\"Marcus Hells\");");
System.out.print("How many guests do you have?\n");
Scanner key = new Scanner(System.in);
int guest_total = key.nextInt();
key.nextLine(); // "Hack" to flush the buffer. Please ignore.
// EXERCISE 1
// start snippet exercise-1-intro
int guest_id;
String guest_name;
int counter = 0;
// end snippet exercise-1-intro
// start snippet exercise-1-batch
while (counter < guest_total) {
// Ask the name of the guest.
System.out.print("Enter name of guest " + (counter + 1) + ".\n");
// Read the name of the guest.
guest_name = key.nextLine();
stmt.addBatch("INSERT INTO GUEST VALUES (" + counter + ", \"" + guest_name + "\", NULL)");
// Add to the batch the statement to insert the required data in the table
counter++;
}
stmt.executeBatch(); // Execute the batch statement.
// end snippet exercise-1-batch
// Solution B
/*
* PreparedStatement ps = conn.prepareStatement("INSERT INTO GUEST VALUES(?, ?, NULL);");
* while (counter < guest_total) {
* System.out.print("Enter name of guest " + (counter + 1) + ".\n");
* guest_name = key.nextLine();
* ps.setInt(1, counter);
* ps.setString(2, guest_name);
* ps.executeUpdate();
* counter++;
* }
*/
// Needed to test our solution to the following two exercises.
stmt.execute("INSERT INTO GUEST VALUES (-1, \"Marcus Hells\", true);");
stmt.execute("INSERT INTO GUEST VALUES (-2, \"Marcus Hells\", false);");
// start snippet exercise-2
ResultSet rset =
stmt.executeQuery(
"SELECT * FROM GUEST, BLACKLIST WHERE GUEST.Name = BLACKLIST.Name AND"
+ " GUEST.Confirmed = true");
if (rset.next()) {
System.out.print(
"Oh no, (at least) one of the guest from the black list confirmed their presence!\n"
+ "The name of the first one is "
+ rset.getString(2)
+ ".\n");
}
// end snippet exercise-2
// start snippet exercise-3
System.out.print(
"Do you want to remove all the guests that are on the black list and confirmed their"
+ " presence? Enter \"Y\" for yes, anything else for no.\n");
if (key.nextLine().equals("Y")) {
stmt.execute(
"DELETE FROM GUEST WHERE NAME IN (SELECT NAME FROM BLACKLIST) AND Confirmed = true;");
}
// end snippet exercise-3
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
15398 |
ee75155d2d99639acd17d31b2cc23cd752078e7e |
CONTRIB.md |
100644 |
blob |
20625 |
25b8e45e7f103089fb70fae5a219f09a29ef5312 |
KNOWN_BUGS.md |
100644 |
blob |
17217 |
e5c1f9f898cca948da42333b100e331d62b61d3c |
LICENSE.md |
100644 |
blob |
1997 |
f8801648fd4ba5843a2cbca8b10e4f69ba5d9b25 |
Makefile |
100644 |
blob |
6695 |
0b91924ffc7b73e2d36150369d4fd41a44b099c5 |
README.md |
040000 |
tree |
- |
eb7afc38251ada69e1967e1ce3e49967eca2267c |
install |
040000 |
tree |
- |
f16b283429b64b620b3bd7681a446ff54d504f84 |
notes |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/caubert/CSCI_3410
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/caubert/CSCI_3410
Clone this repository using git:
git clone git://git.rocketgit.com/user/caubert/CSCI_3410
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main