消除异常

时间:2012-03-24 07:07:55

标签: java jlist listselectionlistener

您好我有以下代码删除sql表中的行。用户单击jlist中的项目。 jlist中的项目将被删除。

public class listdisplay implements ActionListener, ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent a) {

        Object obj = lstcourses.getSelectedValue() ;
        String obj1 = obj.toString() ;
    //  System.out.println(obj1) ; 

        String sql= " SELECT  *" +
                " FROM courseofferinguom " +
                " WHERE professorid=" +
                " '"+teacher.getTeacherIDint()+"'" +
                " AND coursenumber = (SELECT coursenumber "+"FROM courseuom WHERE coursename = '"+obj1+"') " ;


        try {

            statement = con.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();


            if (rs != null) {

                while(rs.next())
                {
                    txtcnum.setText(rs.getString(1)) ;
                    txtdaysoftheweek.setText(rs.getString(4)) ;
                    txttime.setText(rs.getString(5)) ;
                    txtlocation.setText(rs.getString(6)) ;
                    txtcredithours.setText(rs.getString(7)) ;

                    String open = "Open" ;

                    if ( rs.getString(8).equals(open)) {

                        chkopen.setSelected(true); 

                    }



                }
            }



        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    @Override
    public void actionPerformed(ActionEvent e) {


        Object obj = lstcourses.getSelectedValue() ;
        String obj1 = obj.toString() ;

        String sql= " DELETE " +
                " FROM courseofferinguom " +
                " WHERE professorid=" +
                " '"+teacher.getTeacherIDint()+"'" +
                " AND coursenumber = (SELECT coursenumber "+"FROM courseuom WHERE coursename = '"+obj1+"') " ;


        try {

            statement = con.prepareStatement(sql) ;
            int rs = statement.executeUpdate() ;

            txtcnum.setText(" ") ;
            txtdaysoftheweek.setText(" ") ;
            txttime.setText(" ") ;
            txtlocation.setText(" ") ;
            txtcredithours.setText(" ") ;
            chkopen.setSelected(false);



        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // refresh the jlist

        DefaultListModel listModel = new DefaultListModel();
        try{

            statement = con.prepareStatement("SELECT coursenumber "+"FROM courseofferinguom WHERE professorid = '"+teacher.getTeacherID()+"'");
            ResultSet rs1 = statement.executeQuery();

            if (rs1 != null) {

                while(rs1.next())
                {
                    statement = con.prepareStatement("SELECT coursename "+"FROM courseuom WHERE coursenumber = '"+rs1.getString(1)+"'");
                    ResultSet rs2 = statement.executeQuery();


                    if (rs2 != null) {

                        while(rs2.next())
                        {
                            listModel.addElement(rs2.getString(1)) ;

                        }

                    }

                }
            }

            lstcourses.setModel(listModel) ; 

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // clears the textboxes. 





    }

}

但是,正如您所注意到的,有一个valueChanged侦听器。因此,当我删除该项时,它不再在jlist中,因此会创建一个异常。删除工作正常,我只是想消除异常。

1 个答案:

答案 0 :(得分:0)

您可以先进行null检查,然后再继续进行valueChanged(ListSelectionEvent)监听,因为删除记录时该值为null

虽然可以提供更好的解决方案。