【问题标题】:Insert data from table1 to table 2 when table 1 quantity field greater than 0当表1数量字段大于0时从表1向表2插入数据
【发布时间】:2020-03-28 05:58:09
【问题描述】:

我有两张桌子(lib_bookissue_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();
        }
    });

【问题讨论】:

    标签: java swing select insert jtable


    【解决方案1】:

    您的线路:

    rs=stmt.executeQuery(query);
    

    返回 ResultSet 类型的对象(参见 https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html

    要获得数量的价值,您应该使用:

    rs.getInt("Quantity") 
    

    (假设“数量”是整数...)

    【讨论】:

    • thanx dzwiedziuu,我照你说的做了,但它仍然显示“java.lang.NullPointerException”
    猜你喜欢
    • 2019-04-16
    • 2020-10-01
    • 2015-10-30
    • 2019-07-06
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2019-09-28
    相关资源
    最近更新 更多