-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathADDABOOK.java
170 lines (136 loc) · 4.91 KB
/
ADDABOOK.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package net.javaguides.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ADDABOOK extends JFrame implements ActionListener {
// GUI elements
private JTextField idField, titleField, authorField, floorField, shelfField;
private JButton addButton, removeButton;
private JLabel statusLabel;
// Database connection details
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/swing_demo";
static final String USER = "root";
static final String PASS = "Aben5099";
public ADDABOOK(String userSes, UpdateBooks updateBooksFrame) {
// Set up the GUI
super("ADDABOOK");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 2, 10, 10));
panel.add(new JLabel("Book ID:"));
idField = new JTextField();
panel.add(idField);
panel.add(new JLabel("Title:"));
titleField = new JTextField();
panel.add(titleField);
panel.add(new JLabel("Author:"));
authorField = new JTextField();
panel.add(authorField);
panel.add(new JLabel("Floor:"));
floorField = new JTextField();
panel.add(floorField);
panel.add(new JLabel("Shelf:"));
shelfField = new JTextField();
panel.add(shelfField);
addButton = new JButton("Add Book");
addButton.addActionListener(this);
panel.add(addButton);
removeButton = new JButton("Remove Book");
removeButton.addActionListener(this);
panel.add(removeButton);
statusLabel = new JLabel("");
panel.add(statusLabel);
add(panel);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
updateBooksFrame.toFront();
}
});
}
// Handle button clicks
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addBook();
} else if (e.getSource() == removeButton) {
removeBook();
}
}
// Add a new book to the database
private void addBook() {
try {
// Connect to the database
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Prepare the SQL statement
String sql = "INSERT INTO books (book_id, book_title, author, floor, shelf) VALUES (?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, Integer.parseInt(idField.getText()));
stmt.setString(2, titleField.getText());
stmt.setString(3, authorField.getText());
stmt.setInt(4, Integer.parseInt(floorField.getText()));
stmt.setString(5, shelfField.getText());
// Execute the statement
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
statusLabel.setText("Book added to database.");
} else {
statusLabel.setText("Error adding book.");
}
// Clean up
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
statusLabel.setText("Error adding book.");
}
}
// Remove an existing book from the database
private void removeBook() {
try {
// Connect to the database
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Prepare the SQL statement
String sql = "DELETE FROM books WHERE book_id=?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, Integer.parseInt(idField.getText()));
// Execute the statement
int rowsDeleted = stmt.executeUpdate();
if (rowsDeleted >0) {
statusLabel.setText("Book removed from database.");
} else {
statusLabel.setText("Error removing book.");
}
// Clean up
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
statusLabel.setText("Error removing book.");
}
}
public static void main(String[] args) {
// Get the user session from the command line arguments
String userSes = args[0];
// Create the ADDABOOK window
ADDABOOK window = new ADDABOOK(userSes, null);
}
}
// Note: This code assumes that the "books" table already exists in the "swing_demo" database, and that it has the following schema:
// CREATE TABLE books (
// book_id INT NOT NULL,
// book_title VARCHAR(255),
// author VARCHAR(255),
// floor INT,
// shelf VARCHAR(10),
// PRIMARY KEY (book_id)
// );