database_design.zip
立即下载
资源介绍:
database_design.zip
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Borrowing_records extends JFrame {
private JTextField txtBorrowingID;
private JTextField txtBorrowingTime;
private JTextField txtReturnTime;
private JTextField txtWhetherToReturn;
private JTextField txtReaderID;
private JTextField txtISBN;
private JButton btnSearch;
private JButton btnUpdate;
private JButton btnViewAll;
private JButton btnAddNew;
public Borrowing_records() {
borrowing_recordsGUI();
borrowing_records_setupEvents();
}
private void borrowing_recordsGUI() {
setTitle("Borrowing Management System");
setSize(600, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(8, 2, 10, 10));
JLabel lblBorrowingID = new JLabel("Borrowing ID:");
txtBorrowingID = new JTextField(20);
JLabel lblBorrowingTime = new JLabel("Borrowing Time:");
txtBorrowingTime = new JTextField(20);
JLabel lblReturnTime = new JLabel("Return Time:");
txtReturnTime = new JTextField(20);
JLabel lblWhetherToReturn = new JLabel("Whether To Return:");
txtWhetherToReturn = new JTextField(20);
JLabel lblReaderID = new JLabel("Reader ID:");
txtReaderID = new JTextField(20);
JLabel lblISBN = new JLabel("ISBN ID:");
txtISBN = new JTextField(20);
btnSearch = new JButton("Search");
btnUpdate = new JButton("Update");
btnViewAll = new JButton("View All");
btnAddNew = new JButton("Add New");
panel.add(lblBorrowingID);
panel.add(txtBorrowingID);
panel.add(lblBorrowingTime);
panel.add(txtBorrowingTime);
panel.add(lblReturnTime);
panel.add(txtReturnTime);
panel.add(lblWhetherToReturn);
panel.add(txtWhetherToReturn);
panel.add(lblReaderID);
panel.add(txtReaderID);
panel.add(lblISBN);
panel.add(txtISBN);
panel.add(btnSearch);
panel.add(btnUpdate);
panel.add(btnViewAll);
panel.add(btnAddNew);
add(panel);
setVisible(true);
}
private void borrowing_records_setupEvents() {
btnSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchBorrowing();
}
});
btnUpdate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateBorrowing();
}
});
btnViewAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewAllBorrowings();
}
});
btnAddNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addNewBorrowing();
}
});
}
private void searchBorrowing() {
String borrowingID = txtBorrowingID.getText().trim();
if (borrowingID.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter Borrowing ID to search.");
return;
}
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM borrowing_records WHERE borrowing_ID = ?")) {
stmt.setString(1, borrowingID);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
txtBorrowingTime.setText(rs.getString("borrowing_time"));
txtReturnTime.setText(rs.getString("return_time"));
txtWhetherToReturn.setText(rs.getString("whether_to_return"));
txtReaderID.setText(rs.getString("reader_ID"));
txtISBN.setText(rs.getString("ISBN_ID"));
} else {
JOptionPane.showMessageDialog(this, "Borrowing not found.");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage());
}
}
private void updateBorrowing() {
String borrowingID = txtBorrowingID.getText().trim();
if (borrowingID.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter Borrowing ID to update.");
return;
}
String borrowingTime = txtBorrowingTime.getText().trim();
String returnTime = txtReturnTime.getText().trim();
String whetherToReturn = txtWhetherToReturn.getText().trim();
String readerID = txtReaderID.getText().trim();
String ISBN = txtISBN.getText().trim();
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement("UPDATE borrowing_records SET borrowing_time = ?, return_time = ?, whether_to_return = ?, reader_ID = ?, ISBN_ID = ? WHERE borrowing_ID = ?")) {
stmt.setString(1, borrowingTime);
stmt.setString(2, returnTime);
stmt.setString(3, whetherToReturn);
stmt.setString(4, readerID);
stmt.setString(5, ISBN);
stmt.setString(6, borrowingID);
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated > 0) {
JOptionPane.showMessageDialog(this, "Borrowing updated successfully.");
} else {
JOptionPane.showMessageDialog(this, "Failed to update borrowing.");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage());
}
}
private void viewAllBorrowings() {
try (Connection conn = DBConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM borrowing_records")) {
JFrame frame = new JFrame("All Borrowings");
DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "Borrowing Time", "Return Time", "Whether To Return", "Reader ID", "ISBN ID"}, 0);
JTable table = new JTable(model);
while (rs.next()) {
Object[] row = {rs.getString("borrowing_ID"),
rs.getString("borrowing_time"),
rs.getString("return_time"),
rs.getString("whether_to_return"),
rs.getString("reader_ID"),
rs.getString("ISBN_ID")};
model.addRow(row);
}
frame.add(new JScrollPane(table));
frame.setSize(600, 400);
frame.setVisible(true);
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage());
}
}
private void addNewBorrowing() {
String borrowingID = txtBorrowingID.getText().trim();
String borrowingTime = txtBorrowingTime.getText().trim();
String returnTime = txtReturnTime.getText().trim();
String whetherToReturn = txtWhetherToReturn.getText().trim();
String readerID = txtReaderID.getText().trim();
String ISBN = txtISBN.getText().trim();
if (borrowingID.isEmpty() || borrowingTime.isEmpty() || returnTime.isEmpty() || whetherToReturn.isEmpty() || readerID.isEmpty() || ISBN.isEmpty()) {
JOptionPane.showMessageDialog(this, "All fields are required to add a