2010-11-13 44 views
0

我一直在這一段時間,並嘗試過不同的例子(包括我在這裏找到的幾個例子),並試圖使它們適應我所需要的。數據庫查詢未填充到組合框...爲什麼?

我想在一個servlet中使用數據庫查詢來填充生成的HTML窗體中的組合框。當它全部編譯並且頁面彈出一個組合框時,框中沒有任何東西可供選擇,這告訴我如何傳遞變量有什麼問題。

我已經基本上將我的方法縮小到了兩個,但是從兩者都得到了相同的結果。
有人可以看一看,給我一個線索嗎?

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>"`); 

而另一種方法:

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>"`); 

回答

0

你在哪裏設置「home_id」請求屬性,你爲什麼設置它?我認爲你應該拿出下面這行代碼,看看它是否有效。修改你的代碼。

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); 
     } 
    } 
} 
+0

好吧...讓我試試吧 – gcurrier 2010-11-13 21:30:05

+0

其實,如果我刪除那一行,for-each循環中的變量「soldWhich」變得沒有被初始化......嘆氣......這個東西被獲取由分鐘越來越複雜... – gcurrier 2010-11-13 21:31:49

+0

定義列表 sold在try塊之前,並在try塊內取出列表。 – CoolBeans 2010-11-13 21:35:03

0

我想你實際上並沒有得到值(SH)在 「爲(字符串SH:soldWhich)」 循環。你可以嘗試打印一個簡單的表中的值,只是爲了看看你是否真的獲得數據?另外你爲什麼不使用jstl來執行這個任務?而且您的catch塊不會記錄任何錯誤,以防您實際上遇到某些會被忽視的錯誤。

+0

確實,我沒有獲得價值。當我離開第二個catch塊時,我在for-eacc循環行上得到一個空指針異常。 – gcurrier 2010-11-13 20:20:51

+0

此外,我不熟悉使用JSP,不太清楚如何去改變一切以適應(從html到jsp到servlet)。我基本上(正如你可以告訴)從一個html頁面到servlet。沒有選擇和選項塊,我可以爲任何查詢顯示數據(提供sql是正確的),但試圖將結果集放入組合框中,此刻正在擊敗我。我真的可以使用一些幫助... – gcurrier 2010-11-13 20:24:20