Pre-select JSP Form from Database

Pre-select JSP Form from Database

This tutorial will show how to pre-select form input types using data from the database. This will be done using JSTL. To begin, you need to ensure you have the required tag libraries and jar file imported and a connection to your database is established. A quick tutorial on how to do that can be found here. If you’re already set up, lets get straight into it!

To begin, I created a table called my_table using the query below. As you can see, it has 3 entries in the fruits column, 2 of which are round.

query table

You will now see how to pre-select radio buttons, checkboxes and dropdown lists.

Pre-select radio buttons from the database

Pre-select radio buttons with data from the database using this simple step. Notice that if the SQL query returns more than one row, only, the last returned row is what will be selected.

The final form would look like this:

form

Pre-select checkboxes from the database

Pre-select checkboxes with data from the database using this simple step.

The final form would look like this:

Pre-select drop down list items from the database

Pre-select dropdown lists with data from the database using this simple step. Notice that if the SQL query returns more than one row, only, the last returned row is what will be selected.

The final form would look like this:

form

Now you’re all set to create your form and pre-populate its input types with data straight from the database!

Seda
Seda Kunda is a web designer and developer with a degree in Computer Science and a great passion for code. Besides code, she enjoys pepperoni pizza, watching the bachelor and sleeping in on Saturdays.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn