I am implementing the search book frame in my library management system project. In this frame, I want to check the availability of the book. I have two database tables:
1 Book:-which keeps the records of all the books in the library 2 Issuebook;-which keeps the record of issued book
When I am running the frame, the JTable
gets populated with the same value thrice. The output is attached after the code. I am unable to find the problem. Here's my code:
public void actionPerformed(ActionEvent e) {
String bookname=tfBookName.getText();
int count=0;
try{
con=DemoConnection.getConnection();
ps=con.prepareStatement("select book.bookid from book,issuebook where book.bookid!=issuebook.bookid and bookname=?");
ps.setString(1, bookname);
rs=ps.executeQuery();
while(rs.next())
{
count++;
String bookid =rs.getString(1);
String availability="Available";
Object[] row = { bookid, bookname, availability};
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.insertRow(0,row);
}
ps1=con.prepareStatement("select book.bookid from book,issuebook where book.bookid=issuebook.bookid and bookname=?");
ps1.setString(1, bookname);
rs1=ps1.executeQuery();
while(rs1.next())
{
String bookid =rs1.getString(1);
String availability="Issued";
Object[] row = { bookid, bookname, availability};
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.insertRow(0,row);
}
if(rs==null&&rs1==null)
{
JOptionPane.showMessageDialog(frame, "Book named"+bookname+"does not exist!!");
}
else if(rs==null&&rs!=null)
{
JOptionPane.showMessageDialog(frame, "All the copies of"+bookname+" book are issued!!");
}
else if(rs!=null&&rs==null)
{
JOptionPane.showMessageDialog(frame, "All the copies of"+bookname+" book are avaialble!!");
}
else if(rs!=null&&rs!=null)
{
JOptionPane.showMessageDialog(frame, count+" copies of"+bookname+" book is avaliable!!");
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
Here's output. When the book which is issued, it is shown in the table; no duplicate entries were filled up, but duplicate entries get filled in table for the available books.