根据数据库表中的行数向组合框添加值

我又遇到了更多的代码问题。这次,我想根据数据库表中有多少行向Combobox动态添加选项。

我已经尝试了我在网上找到的用于此功能的代码段。我还想返回int变量,以使其保留在代码中供以后使用:将新行添加到表中时,会将新选项添加​​到组合框中以供该行中的以后更新。

private int CountRows(){

    int rowCount = 0;
    int i;
    try{
        String url="jdbc:oracle:thin:@//localhost:1521/orcl";
        Statement stmt = null;
        OracleDataSource ods = new OracleDataSource();
        ods.setURL(url);
        ods.setUser("LAB_NICK");
        ods.setPassword("10Niroxxe98");
        ResultSet rs = null;
        Connection conn = ods.getconnection();
        rs = stmt.executeQuery("SELECT * FROM strada");
        rs.next();
        rowCount = rs.getInt(1);
        rs.close();
        stmt.close();
        conn.close();
    }catch(Exception e){
        JOptionPane.showmessagedialog(null,e);
    }
    rowCount++;
    for(i=1;i<=rowCount;i++){
       String RoadNumberToAdd = Integer.toString(i);
       roadNum.addItem(RoadNumberToAdd);
    }
    return rowCount;
}

例如,如果数据库表中有4行,则该函数应返回4,以及从1到4的组合框选项。可悲的是,该函数仅返回可怕的NullPointerException

kyj09 回答:根据数据库表中的行数向组合框添加值

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3167550.html

大家都在问