-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOfflineLibrary.java
More file actions
101 lines (76 loc) · 2.99 KB
/
OfflineLibrary.java
File metadata and controls
101 lines (76 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
public class OfflineLibrary {
private static final String DB_URL = "jdbc:mysql://localhost:3306/library";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "Aditya1218";
public static void main(String[] args) throws SQLException{
try (Scanner scanner = new Scanner(System.in);
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement()) {
int choice;
do {
displayMenu();
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
readBooks(conn ,scanner);
break;
case 2:
listBooks(conn, scanner);
break;
case 0:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 0);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void displayMenu()
{
System.out.println(" ");
System.out.println(" ");
System.out.println("Offline Free Library");
System.out.println("1. Read a Book");
System.out.println("2. List Books");
System.out.println("0. Exit");
}
private static void listBooks(Connection conn, Scanner scanner) throws SQLException {
System.out.println(" ");
System.out.println("List of Books:");
System.out.println("Enter the SCHOOL");
String SCHOOL = scanner.next();
scanner.nextLine();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + SCHOOL);
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
System.out.println(id + ". " + title + " || " + author + " || ");
}
}
private static void readBooks(Connection conn, Scanner scanner) throws SQLException {
listBooks(conn, scanner);
System.out.println(" ");
System.out.println("Enter the BOOK");
String filename = scanner.nextLine().toLowerCase() + ".txt";
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
System.out.println("Content of " + filename + ":");
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}