程序中出现例外情况

时间:2017-10-04 22:46:53

标签: java

public void fillCombobox()
    {
        try{
            String strbox="select * from details";
            PreparedStatement pst=con.prepareStatement(strbox);
            ResultSet rs2=pst.executeQuery();
            while(rs2.next())
            {
                comBox.addItem(rs2.getString("name"));
            }
        con.close();
        }
        catch(Exception e4)
        {
            JOptionPane.showMessageDialog(null,"ERROR" );

        }
    }

在上面的代码中,异常即将到来,并且JOptionPane会显示ERROR消息。我已经与Mysql建立了联系。

1 个答案:

答案 0 :(得分:-1)

这是一个来自Java Derby SQL数据库的工作组合框填充

我相信你必须将rs2.getstring分配给变量然后将变量传递给box.additem。

 public static void ViewClass() throws ClassNotFoundException, SQLException
    {

    //This inserts class table

        Connection connection3 = DriverManager.getConnection(JDBC_URL);


        String sql3 = "SELECT class_ FROM CLASS_ ORDER BY class_";

    try {


        PreparedStatement ps2 = connection3.prepareStatement(sql3);


        // execute select SQL stetement
        ResultSet rs = ps2.executeQuery();

        while (rs.next()) {

            String existingClass = rs.getString("class_");
                           // add to combobox

                         classEnter.addItem(existingClass);



            System.out.println("classes loaded to combo box");


        }
                    ps2.close();
                    connection3.close();

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    } 




    System.out.println("Class record sucessfully retrieved");
  }