Press "Enter" to skip to content

Update and delete book of library management system.

Update and delete Statistics is very important for a library management system.

It is final post of this project and I think it will be very useful to learner. Any student can easily use this code and make his/her project completely.

To get help you can see the video.

Source code for add new student in library page.

  1. Connect with connection class…
Connection con;
ResultSet rs;
PreparedStatement pst;

public Update() {
initComponents();
con = Connect.dbConnect();
middle();
refresh();
}

public void middle() {
setLocationRelativeTo(null);
}

2. Refresh book code…

public void refresh() {
try {
String query = "select * from book";
pst = con.prepareStatement(query);
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
}
jTextFieldBookID.setText("");
jTextField_Name.setText("");
jTextField_Editor.setText("");
jTextField_Publisher.setText("");
jTextField_Price.setText("");
jTextField_Pages.setText(null);
refresh();

3. Delete book code…

String sql = "delete from book where Book_ID=?";
try {
pst = con.prepareStatement(sql);
pst.setString(1, jTextFieldBookID.getText());
pst.execute();
refresh();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}

4. Update book code…

if (jTextFieldBookID.getText().equals("") || jTextField_Name.getText().equals("") || jTextField_Editor.getText().equals("") || jTextField_Publisher.getText().equals("") || jTextField_Price.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Field Must Not Empty...!!!");
} else {
String B_ID, name, editor, publisher, price, pages;
B_ID = jTextFieldBookID.getText();
name = jTextField_Name.getText();
editor = jTextField_Editor.getText();
publisher = jTextField_Publisher.getText();
price = jTextField_Price.getText();
pages = jTextField_Pages.getText();
try {
String query = "Update book set Book_ID='" + B_ID + "', Name='" + name + "', Edition='" + editor + "', Publisher='" + publisher + "', Price='" + price + "', Pages='" + pages + "' where Book_ID='" + B_ID + "' ";
pst = con.prepareStatement(query);
pst.execute();
refresh();
JOptionPane.showMessageDialog(null, "Book Updated Succesfully...!!!");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}

5. Search book.

try {
int row = jTable1.getSelectedRow();
String BookID = (jTable1.getModel().getValueAt(row, 1)).toString();
String query = "select * from book where Book_ID='" + BookID + "' ";
pst = con.prepareStatement(query);
rs = pst.executeQuery();

while (rs.next()) {
jTextFieldBookID.setText(rs.getString("Book_ID"));
jTextField_Name.setText(rs.getString("Name"));
jTextField_Editor.setText(rs.getString("Edition"));
jTextField_Publisher.setText(rs.getString("Publisher"));
jTextField_Price.setText(rs.getString("Price"));
jTextField_Pages.setText(rs.getString("Pages"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}

Interface of after run the update, delete,refresh and search book page.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *