我有两个表(lib_book
和issue_book
)。每当我发行书籍时,我都会从lib_book
中减少“数量”值。当数量达到“ 0”时,我想停止在表2中插入,并显示“书在图书馆中不可用”。当我执行此操作时,会抛出NullPointerException
,并且不确定如何防止它发生。
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library?allowMultiQueries=true","root","");
query="select Quantity from lib_book where BookId='"+t1.getText()+"';";
rs=stmt.executeQuery(query);
if(rs.equals("0"))
{
JOptionPane.showMessageDialog(null,"Book not Available");
}
else
{
query="insert into issue_book values('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+t4.getText()+"','"+t5.getText()+"','"+t6.getText()+"','"+l11.getText()+"');";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Book Issued Successfully");
query="update lib_book set Quantity=Quantity-1 where BookId='"+t1.getText()+"';";
stmt.executeUpdate(query);
}
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}
showTableData();
showTable2Data();
}
});
您的电话:
rs=stmt.executeQuery(query);
返回类型为ResultSet的对象(请参见https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html]
要获得数量的价值,您应该使用:
rs.getInt("Quantity")
((假设'数量'是整数...)