-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookStore.java
More file actions
155 lines (142 loc) · 5.83 KB
/
BookStore.java
File metadata and controls
155 lines (142 loc) · 5.83 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.sql.*;
import java.util.Scanner;
//testing commit
public class BookStore {
//create scanner objects
Scanner scan2 = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
Scanner scan3 = new Scanner(System.in);
//menu to print out to the user
void menu(){
System.out.println("Please make a choice below: ");
System.out.println("1. Enter book\n" +
"2. Remove book \n" +
"3. Update book\n" +
"4. Search book\n" +
"0. Exit ");
int choice = scan2.nextInt();
//switch statment to use method that the user selects
switch(choice){
case 1: enterBook();
break;
case 2: deleteBook();
break;
case 3: updateBook();
break;
case 4: searchBook();
break;
case 0: System.exit(0);
break;
}
}
//enter book method
private void enterBook(){
//access database
try (
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bookstore?useSSL=false", "myuser",
"password");
Statement stmt = conn.createStatement();
){
//output to the user and take input
System.out.println("Enter all the information of the book you would like to add.");
String value = scan.nextLine();
String sqlEnterBook = "insert into bookstock "
+ "values" + "("+ value + ")";
//execute sql command
int countInserted = stmt.executeUpdate(sqlEnterBook);
System.out.println(countInserted + " records inserted.\n");
}catch(SQLException e){
e.printStackTrace();
}
}
//delete book method
private void deleteBook(){
try (
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bookstore?useSSL=false", "myuser",
"password");
Statement stmt = conn.createStatement();
){
//take input from user and execute the sql command
System.out.println("Enter the id of the book you want to delete: ");
String primaryKey = scan.nextLine();
String sqlDeletebook = "delete from bookstock where id = " +primaryKey;
int countDeleted = stmt.executeUpdate(sqlDeletebook);
System.out.println(countDeleted + "records deleted");
}catch(SQLException e){
e.printStackTrace();
}
}
//update book method
private void updateBook(){
//access database
try (
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bookstore?useSSL=false", "myuser",
"password");
Statement stmt = conn.createStatement();
){
//take input from the user and execute sql command
System.out.println("Enter the id of the book you need to update: ");
String enterId = scan.nextLine();
System.out.println("Enter the information you want to change: ");
String enterInfo = scan3.nextLine();
String sqlUpdateBook = "update bookstock set " +enterInfo+ " where id = " +enterId;
int countUpdated = stmt.executeUpdate(sqlUpdateBook);
System.out.println(countUpdated + " record updated");
}catch(SQLException e){
e.printStackTrace();
}
}
//search book method
private void searchBook(){
//access databsse
try (
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/bookstore?useSSL=false", "myuser",
"password");
Statement stmt = conn.createStatement();
){
//print out menu
int choice= scan2.nextInt();
String sqlSearchBook = "";
String enterinfo;
System.out.println("1. Search by title\n 2.Search by ID\n 3.Search by author");
//switch statement to create alternate sql commands
switch(choice){
case 1:
System.out.println("Enter title: ");
enterinfo = scan.nextLine();
sqlSearchBook = "select * from bookstock where title = " +"'"+ enterinfo+ "'";
break;
case 2:
System.out.println("Enter id: ");
enterinfo = scan2.nextLine();
sqlSearchBook = "select * from bookstock where id = " +enterinfo;
break;
case 3:
System.out.println("Enter author: ");
enterinfo = scan3.nextLine();
sqlSearchBook = "select * from bookstock where author = " +"'"+ enterinfo+ "'";
break;
}
//execute sql statement
ResultSet rset = stmt.executeQuery(sqlSearchBook);
System.out.println("The records selected are: ");
int rowCount=0;
//print out records
while(rset.next()) {
String title = rset.getString("title");
int qty = rset.getInt("qty");
int id = rset.getInt("id");
String author = rset.getString("author");
System.out.println(id+", "+title + ", "+author+ "," + qty);
++rowCount;
}
//catch exception if database not found
}catch(SQLException e){
e.printStackTrace();
}
}
}