I am trying to create a dropdown that is populated by values from MySQL DB. In DB I have defined time - price values.
Example from SQL table
-----------------------
| id | time | price |
-----------------------
| 1 |13:00 | 6 |
| 2 |14:00 | 12 |
| 3 |15:00 | 18 |
| 4 |16:00 | 24 |
-----------------------
The goal is when I choose a specific time, the price will be displayed.
I have managed to get the dropdown working with time values, but how to get a price from the table as to be displayed.
So, if I choose time to be 15:00
I would like that price value is displayed under the dropdown menu.
Here is a code snippet from JSP page (index.jsp):
<form>
Select time:
<select>
<c:forEach items = "${LIST}" var = "list">
<option value = "${list.time}">${list.time}</option>
</c:forEach>
</select>
<br><br>
<input type = "submit" value = "Confirm">
</form>
Dropdown values, getting values from DB (model class is used as well, not displayed in this example):
public List<model> list() throws Exception{
List<model> list = new ArrayList<>();
Connection myConn = null;
Statement myStmt = null;
ResultSet myRes = null;
try {
//get connection
myConn = dataSource.getConnection();
//create sql query in string
String sql = "SELECT * FROM price_time_data ORDER BY id";
myStmt = myConn.createStatement();
//execute statement
myRes = myStmt.executeQuery(sql);
//Process results from query
while(myRes.next()) {
//retrieve row by row
int id = myRes.getInt("id");
String time = myRes.getString("time");
String price = myRes.getString("price");
//create list object
model tempModel = new model(id, time, price);
//add it to list
list.add(tempModel);
System.out.println("Success!!!!");
}
return list;
} finally {
//close JDBC object
close(myConn, myStmt, myRes);
}
Passing values to JSP via servlet (doGet):
// get values from db util
List<model> list = DataBaseDAO.list();
// add values to the report
request.setAttribute("LIST", list);
// send values to JSP page
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
For submission, I will need price and time values, but I can get that from the table directly. Live onchange price display would be more user friendly.
Any help is welcome.
Thanks.