private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        DefaultTableModel model1 = (DefaultTableModel) jTable2.getModel();
        int bookcode = 0;
        int n = 0;
        String bookname = null;
        String query;
        int issuerid;
        int count;
        int row = jTable2.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(this, "Please Select a Book");
        } else {
            bookcode = (Integer) model1.getValueAt(row, 0);
            bookname = (String) model1.getValueAt(row, 1);
        }

        if (row != -1) {
            try {
                
                query = "select username,issuerid from users where username = '" + username1 + "';";
                ResultSet rs1 = MyQueries.excQuery(query);
                rs1.next();
                username1 = rs1.getString("username");
                issuerid = rs1.getInt("issuerid");
                myClasses.Connections.close(); // Close connection to prevent Database lock
                
                query = "select count(*) from accounts where username = '" + username1 + "';";
                ResultSet rs2 = MyQueries.excQuery(query);
                rs2.next();
                count = rs2.getInt("count(*)");
                myClasses.Connections.close(); // Close connection to prevent Database lock
                
                query = "select * from settings;";
                ResultSet rs3 = MyQueries.excQuery(query);
                rs3.next();
                int maxbooks = rs3.getInt("maxbooks");
                myClasses.Connections.close(); // Close connection to prevent Database lock

                if (count >= maxbooks) 
                {
                    JOptionPane.showMessageDialog(this, "Sorry! You cannot Issue more than " + maxbooks + " books");
                } 
                else 
                {
                    String idate = MyDate.issueDate();
                    String rdate = MyDate.returnDate();
                    query = "insert into accounts values(" + issuerid + ",'" + username1 + "'," + bookcode + ",'" + bookname + "','" + idate + "','" + rdate + "');";
                    MyQueries.excUpdate(query);
                    myClasses.Connections.close(); // Close connection to prevent Database lock
                    JOptionPane.showMessageDialog(this, "Book Succesfully issued");
                }
            } 
            catch (SQLException | HeadlessException f) 
            {
                if (f.getMessage().equals("Duplicate entry '" + bookcode + "' for key 'PRIMARY'")) 
                {
                    JOptionPane.showMessageDialog(this, "This book has already been issued by you or someone else");
                } 
                else
                {
                    JOptionPane.showMessageDialog(this, f.getMessage());
                }
            }

        }
    }//GEN-LAST:event_jButton2ActionPerformed