简体   繁体   English

数据库查询未填充到组合框...为什么?

[英]DB query not populating to combo box…why?

I've been at this for awhile and have tried different examples (including a few that I found here), and tried to adapt them to what I need. 我已经有一段时间了,尝试了不同的示例(包括我在这里找到的一些示例),并尝试使它们适应我的需要。

I am trying to use a DB query in a servlet to populate a combo box in the resulting html form that is created. 我试图在servlet中使用数据库查询来填充创建的html表单中的组合框。 While it all compiles and the page pops up with a combobox, there is nothing in the box to choose from, which tells me that something is wrong with how I am passing variables. 编译完成后,页面上弹出一个组合框,但框中没有可供选择的内容,这告诉我传递变量的方式有问题。

I've basically narrowed my methods down to two, but get the same results from both. 我基本上将方法缩小到两个,但是从这两个方法获得相同的结果。
Can someone have a look-see and give me a clue? 有人可以看看并给我一个提示吗?

out.print(`"<tr><td>SoldWhich Home ID:  `</td><td>"`);
//Query table for results to go into option box  
ResultSet rs1 = null;  
Statement stmt1 = null;  
Connection con1 = null;  

try {
    Class.forName(DRIVER);  
    con1 = DriverManager.getConnection(URL, username, password);  
    String sql = "SELECT home_id FROM Home";  
    stmt1 = con1.createStatement();  
    rs1 = stmt1.executeQuery(sql);  
    ArrayList<String> soldWhich = new ArrayList<String>();
    List<String> soldWhich = new ArrayList<String>();

    while (rs1.next()){
        for (int i=1;i<=rs1.getRow(); i++ ){
            String value = rs1.getString(1);
            soldWhich.add(value);
        }
    }
}
catch(Exception e){}
//Begin option box
out.print(`"<select width=\"150px\" align=\"right\" name=\"soldWhichBox\">"`);
String soldWhich[] = (String[])req.getAttribute("home_id");
//populate with query output
try{
    for(String sh : soldWhich){
        out.print(`"<option width=\"150px\" align=\"right\" value=\""+sh+"\">"+sh+"</option>"`);
    }
    rs1.close();
    con1.close();
}

catch (Exception e){}
out.print(`"</select>"`);
out.print(`"</td></tr>"`);

And the other method: 还有另一种方法:

out.print(`"<tr><td>SoldWhich Home ID:  </td><td>"`);
//Query table for results to go into option box  
ResultSet rs1 = null;  
Statement stmt1 = null;  
Connection con1 = null;  
try{  

    Class.forName(DRIVER);  
    con1 = DriverManager.getConnection(URL, username, password);
    String sql = "SELECT home_id FROM Home ORDER BY home_id";
    stmt1 = con1.createStatement();
    rs1 = stmt1.executeQuery(sql);
    List<String> soldWhich = new ArrayList<String>();

    while (rs1.next()){
       soldWhich.add(rs1.getString(1));
    }
}
catch(Exception e){}
//Begin option box
out.print(`"<select width=\"150px\" align=\"right\" name=\"soldWhichBox\">"`);
String soldWhich[] = (String [])req.getAttribute("home_id");
//populate with query output
try{
    for(String sh : soldWhich){
        out.print(`"<option width=\"150px\" align=\"right\" value=\""+sh+"\">"+sh+"</option>"`);
    }
    rs1.close();
    con1.close();
}
catch (Exception e){}
out.print(`"</select>"`);
out.print(`"</td></tr>"`);

I think you are not actually getting values (sh) in the "for(String sh : soldWhich)" loop. 我认为您实际上并没有在“ for(String sh:soldWhich)”循环中获取值(sh)。 Can you try printing the values in a simple table just to see if you actually get data? 您是否可以尝试在一个简单的表中打印这些值,以查看是否真正获取了数据? Also why aren't you using jstl to perform this task? 另外,为什么不使用jstl执行此任务? And your catch block does not log any errors either in case you are actually getting some sort of errors that will go unnoticed. 并且catch块也不会记录任何错误,以防万一您实际上遇到了某种不会引起注意的错误。

Where are you setting "home_id" request attribute and why are you even setting it? 您在哪里设置“ home_id”请求属性,为什么还要设置它? I think you should take out the following line of code and see if it works. 我认为您应该取出以下代码行,看看它是否有效。 Modify your code to this. 修改您的代码。

List<String> soldWhich = null;
try {
    Class.forName(DRIVER);  
    con1 = DriverManager.getConnection(URL, username, password);  
    String sql = "SELECT home_id FROM Home";  
    stmt1 = con1.createStatement();  
    rs1 = stmt1.executeQuery(sql);  
    soldWhich = new ArrayList<String>();

    while (rs1.next()){
        for (int i=1;i<=rs1.getRow(); i++ ){
            String value = rs1.getString(1);
            soldWhich.add(value);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM