-1

I am working in netbeans, I have a JSP page that has a dropdownlist that needs to pull the options from a MySQL database. I cannot figure out how to get the MySQL database to populate the dropdownlist.

<form action="student/studentQueryResponse.jsp">
    <strong>Select a student:</strong>
    <select name="studentID">
        <c:forEach var="row" items="${student.rowsByIndex}">
            <c:forEach var="column" items="${row}">
                <option <c:out value="${column}"/>
                </option>
            </c:forEach>
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joe Smith
  • 205
  • 4
  • 15

2 Answers2

1

Where did you paste the code from? It uses the rowsByIndex property, so it is clearly meant for use with jstl tag (and it is written by someone who knew it). On the other hand, using two nested forEach clauses makes no sense in this context: you are going to end up with a separate option for each of the students data (name, last name, whatever), instead of a single option for a single student. It looks as if the original code was for a data grid of some kind, and has been modified without a trace of understanding.

With all due respect, I share the view that you are not ready to write the application - and you are not going to learn much by trying, as you will soon come to harder and harder topics. If you are forced to continue the application, try dumping JSF and concentrate on JSP/JSTL, I believe it has less caveats for a beginner and it will make it easier for you to learn the basics of web applications.

That said, the answer you are looking for is:

<sql:setDataSource dataSource="jdbc/db" />
<sql:query var="students">
    select * from students
</sql:query>
<form action="student/studentQueryResponse.jsp">
    <strong>Select a student:</strong>
    <select name="studentID">
        <c:forEach var="row" items="${students.rowsByIndex}">
            <option id="<c:out value="${row[0]}"/>"><c:out value="${row[1]}"/></option>            
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>

You will have to substitute the sql query with your own, and you will have to register a datasource jdbc/db in your application server and in the web.xml file. Of course, you will also need a MySQL driver. I assumed that you willl need student's id and that it is the first column of your query.

fdreger
  • 12,264
  • 1
  • 36
  • 42
  • Note: the JSTL `sql` tag library is intended for quick prototyping or small applications only, not for business applications. This is even explicitly mentioned in [Java EE tutorial](http://download.oracle.com/javaee/5/tutorial/doc/bnald.html). – BalusC Dec 24 '10 at 12:49
  • I could not agree more. And yet, Access, Excel + VBA, and JSTL core + SQL are the few technologies that sometimes produce acceptable results even when used by beginners. True, the projects might require some rewriting later on and they are rarely modifiable by anyone but their author, but they do work. Judging from the question asked, the way it was asked, and the snippet - I do not envision its author creating a stable, well-architectured or even working JSF application without acquiring more experience. But I sort of see him writing a working piece of software with JSP/JSTL. – fdreger Dec 24 '10 at 18:07
0

Yes BalusC's concerns are right, if you are not using JSF then its not a JSF Project. If you make it a JSF project then selectOneItem selectOneMenu can do the trick for you. As far as database connectivity is concerned then clean way would be ideally to make connection in you backing bean, either using datasources or just plain jdbc or other methods.

FUD
  • 5,114
  • 7
  • 39
  • 61