forked from surajgojanur/Bookstore-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookstoreManagementSystem.java
More file actions
94 lines (82 loc) · 3.65 KB
/
Copy pathBookstoreManagementSystem.java
File metadata and controls
94 lines (82 loc) · 3.65 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.ArrayList;
class Book {
String title;
String author;
String publisher;
double cost;
public Book(String title, String author, String publisher, double cost) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.cost = cost;
}
}
class Admin {
ArrayList<Book> books = new ArrayList<>();
public void addBook(String title, String author, String publisher, double cost) {
Book book = new Book(title, author, publisher, cost);
books.add(book);
// Code to insert the book into the database should be added here (using SQL).
}
}
public class BookstoreManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Admin admin = new Admin();
// Database connection parameters
String jdbcUrl = "jdbc:mysql://localhost:3306/your_database"; // Update with your database URL
String jdbcUser = "suraj"; // Update with your database username
String jdbcPassword = "12345"; // Update with your database password
// Initialize the database connection
try {
Connection connection = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
while (true) {
System.out.println("1. Add Book");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.next();
System.out.print("Enter author: ");
String author = scanner.next();
System.out.print("Enter publisher: ");
String publisher = scanner.next();
System.out.print("Enter cost: ");
double cost = scanner.nextDouble();
// Add the book to the database
addBookToDatabase(connection, title, author, publisher, cost);
admin.addBook(title, author, publisher, cost);
System.out.println("Book added successfully.");
break;
case 2:
System.out.println("Exiting the system.");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Failed to connect to the database.");
}
}
// Method to insert a book into the database
private static void addBookToDatabase(Connection connection, String title, String author, String publisher, double cost) throws SQLException {
String insertQuery = "INSERT INTO books (title, author, publisher, cost) VALUES (?, ?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) {
preparedStatement.setString(1, title);
preparedStatement.setString(2, author);
preparedStatement.setString(3, publisher);
preparedStatement.setDouble(4, cost);
preparedStatement.executeUpdate();
}
}
// Created by Suraj
}