【问题标题】:Add data from different columns into dropdown menu in jsp将不同列的数据添加到jsp的下拉菜单中
【发布时间】:2022-01-11 00:02:59
【问题描述】:

所以我想知道是否可以将来自两个不同列的数据添加到下拉菜单中?举个例子,假设我的桌子看起来像 this

我想让我的下拉菜单看起来像this

但我不知道如何更改我的代码

<% 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>

【问题讨论】:

    标签: java mysql jsp drop-down-menu


    【解决方案1】:

    你可以使用多种方式..

    但最简单的可能是
    选择 brad 的 concat 并命名为 name

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

    并且只使用名称结果

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      相关资源
      最近更新 更多