Add data from different columns into dropdown menu in jsp

Viewed 61

So what I wanna know is if it's possible to add data from two different columns into a dropdown menu? To give an example, let's say my table looks like this

And I wanna make my dropdown menu look like this

But I don't know how to change my code

<% try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/autoshop", "root", "carli2016");
Statement st=conn.createStatement();
String query = "select * from cars";
ResultSet rs = st.executeQuery(query);
%>
<select name="car">
<%
while(rs.next()){
String brand = rs.getString("brand");
String name = rs.getString("name");
%>
<option value="<%=brand %>"><%=brand %></option>
<option value="<%=name %>"><%=name %></option>

<%
}
%>
</select>
1 Answers

you can use several way ..

but th simplest could be
select the concat for brad and name as name

    select concat(brand, ' ', name) as name from cars

and use only the name result

<%
while(rs.next()){
String name = rs.getString("name");
%>
<option value="<%=name %>"><%=name %></option>
Related