java - 如何显示一个表中下拉框的第一个值以及另一个表中相同下拉框的其他值

标签 java mysql jdbc

I have display the data from database into textbox and drop down box from users table. I have one display button,when I click on display button then the list of employees appear into textbox perfectly,but it is not working perfectly into drop down box.I am fetching values of dropdown from another table also,but I want to display the respective value of employee department into the drop down box first.

<td><select name="Department" value="<%=pList.get(4)%>" style="width:150px">
         <%
         Statement statement1 = con.createStatement() ;
         resultset1 =statement1.executeQuery("select Department from users") ;
   while(resultset1.next()){ %>
            <option><%= resultset1.getString(1)%></option>

        <% } %>
    </select></td> 

I am getting first value of data from drop down box,but I want to display the data of employee's department as first value into drop down box .I have display the data of employee from users table and department from department table into drop down box,but I want to Display the data from users table as first value into dropdown box,and then I can select another value and update the data.I am not getting ,How can I implement these.

最佳答案

根据我的理解,您希望在下拉列表和文本框中显示记录。 所以回答你的第一个问题:你在代码中错过了“option”标签

Try out below code :

<td><select name="Department1" value="<%=pList.get(4)%>" style="width:150px">
 <%
 Statement statement1 = con.createStatement() ;
 resultset1 =statement1.executeQuery("select Department from users") ;
 while(resultset1.next()){ %> 
<option>
      <%= resultset1.getString(1)%>
</option>

<% } %>

回答你的第二个问题:你想先显示用户数据,然后显示员工数据:

我建议在列表中获取用户记录,然后在同一列表中添加员工记录。

尽量避免在 JSP 中使用 JAVA 代码。

在 JSP 中编写 JAVA 代码不是一个好的做法。

关于java - 如何显示一个表中下拉框的第一个值以及另一个表中相同下拉框的其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48803353/

相关文章:

java - spring boot注释 Autowiring 的问题

java - 清除 Realm 表/数据库的正确方法?

PHP 登录重定向循环

php - 提取 mySQL 数据并放置在 PHP 中的变量中

php - 没有错误: Data is not inserting in PHP MySql from Android

java - 从 JDBC 数据库中检索数据到 Jtable

Java 在 XSL 转换中将 byte[] 发送到 base64Binary

java - 无法显示 jTable 的标题

java - 在 Java 中更新 MySQL 表

postgresql - SchemaSpy PostgreSQL 连接问题