0

私は試しましたが、これは私の試みです 。データベースからjspでコンボボックスに入力する必要があるのを助けてください

1-jsp ページ

       <p class="ppp">Category</p>
<jsp:useBean id="categorys" class="database.conDB" scope="page"/>
<select name="categorys" size="1" style="width:196px; padding:5px;" >
<c:forEach var="categ" items="${categorys.categoryNames} ">
 <option value="${categ}">${categ} </option>
 </c:forEach>  
</select>

=========== 2-これはconDBクラスの私の関数です

    public String[] getCategory()
{
    String query="SELECT category_name FROM books.category;";
    Statement statement;
    ResultSet resultSet ;
    try{
        statement=createConnection().createStatement();
        resultSet=statement.executeQuery(query);
        int count=0;
        while(resultSet.next())
                count++;
        categoryNames=new String[count];
        resultSet.first();
        do{
            categoryNames[count -1]=resultSet.getString("category_name");
            count--;

        }while(resultSet.next());


    }catch(Exception e)
    {e.printStackTrace();}
    for (int i = categoryNames.length; i > 0; i--)
        System.out.println(categoryNames[i - 1]);
    return categoryNames;
    }
4

1 に答える 1

0

selected選択するタグに属性を追加する<option>必要があります。

例えば:

<option value="${categ}" ${ <<selected condition>> ? 'selected' : ''>${categ}</option>

<<selected condition>>カテゴリを選択するかどうかを決定するために使用される条件です。たとえば、常にその名前のカテゴリを選択する場合は、次のhouseようになります。categ == 'house'

于 2013-11-10T21:07:16.323 に答える