我正在使用Java + MySql开发一个图书馆管理系统,并且我试图根据计数器添加图书记录。我只需要允许用户添加图书,直到计数器为> = 10.当它为10时,它应该给出错误消息。如果它低于10,程序应允许用户添加记录。到目前为止,我一直想写这个,但结果不是我对系统的期望。关于改进这一点的任何想法。
private void btn_addActionPerformed(java.awt.event.ActionEvent evt) {
try {
rs = st.executeQuery("SELECT COUNT(*) FROM `read_with_us`.`book_inventory` WHERE book_title='" + txt_booktitle.getText() + "'");
rs.next();
int count = rs.getInt(1);
if (count >=10) {
JOptionPane.showMessageDialog(null, "Sorry Maximum allowable quantity (10) has been reached!!");
}
else {
String sql2 = "INSERT INTO `read_with_us`.`book_inventory` (`book_id`, `book_title`, `isbn_no`, `book_author`, `book_publisher`, `category_id`, `status`) VALUES (?,?,?,?,?,?,?);";
pst = con.prepareStatement(sql2);
pst.setInt(1, Integer.parseInt(txt_bookid.getText()));
pst.setString(2, txt_booktitle.getText());
pst.setString(3, txt_isbnno.getText());
pst.setString(4, txt_author.getText());
pst.setString(5, txt_pubisher.getText());
String category = (String) com_category.getSelectedItem();
pst.setString(6, category);
String status = (String) com_status.getSelectedItem();
pst.setString(7, status);
pst.execute();
//Clearing the fields
txt_booktitle.setText("");
txt_isbnno.setText("");
txt_author.setText("");
txt_pubisher.setText("");
com_category.setSelectedItem("1");
com_status.setSelectedItem("Available");
JOptionPane.showMessageDialog(null, "Successfully Added");
pst.close();
rs.close();
con.close();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error:" + " " + e.getMessage());
e.printStackTrace();
}
}
run:
java.lang.NullPointerException
at Books.btn_addActionPerformed(Books.java:656)
at Books.access$200(Books.java:19)
at Books$3.actionPerformed(Books.java:161)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

答案 0 :(得分:1)
不要像这样使用rs.next()
,而是使用if:
PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM `read_with_us`.`book_inventory` WHERE book_title = ?");
ps.setString(1, txt_booktitle.getText());
rs = ps.executeQuery();
int count = 0;
if(rs.next()){
count = rs.getInt(1);
}