根据数据库查询创建下拉框?

时间:2016-11-15 01:49:40

标签: java jsp

所以我创建了一个在线银行转帐部分,用户可以在其帐户之间转帐。我有一个下拉框,它会显示用户拥有的帐户数量。我不确定我是否正确这样做。到目前为止,这是我的代码:

转印 - page.jsp

 <form name="transfer" action="TransferServlet" method="post">
            <% 
                String email = (String) session.getAttribute("email");
                String getAccountType = UserProfileService.getAccountTypeByEmail(email);
                int count = UserProfileService.getAmountOfAccounts(email);
             %>
            From Account: <select name="AccountType">

                                    <%
                                        for(int i = 0; i < count; i++)
                                        {
                                    %>
                                           <option>  <%=getAccountType %>
                                           <%
                                        }
                                    %> 
                                           </option>
                          </select>
        </form>

数据库类:

 public static String getAccountTypeByEmail(String email)
        {
          //  AccountType accountType = new AccountType();
            String getAccountType = "";
            try
            {
                Connection conn = DBConnection.getConnection();
                String query = "SELECT * FROM accountType WHERE email=?";
                PreparedStatement stmt = conn.prepareStatement(query);
                stmt.setString(1, email);

                ResultSet rs = stmt.executeQuery(query);
                rs.next();

                getAccountType = rs.getString("accountType");

              //  accountType.setAccountType(getAccountType);

            } catch (Exception e)
            {
                System.out.println(e);
            }

          //  return accountType;
          return getAccountType;
        }

        public static int getAmountOfAccounts(String email)
        {
            int count = 0;
            try
            {
                Connection conn = DBConnection.getConnection();
                String query = "SELECT count(*) FROM accountType WHERE email=?";
                PreparedStatement stmt = conn.prepareStatement(query);
                stmt.setString(1, email); 

                ResultSet rs = stmt.executeQuery(query);
                while(rs.next())
                {
                   String account = rs.getString("accountType");
                   count++;
                }

            } catch (Exception e)
            {
                System.out.println(e);
            }

            return count;
        }

1 个答案:

答案 0 :(得分:0)

我不知道在什么情况下您会在下拉框中显示帐户的金额。绑定一张以上银行卡的帐户? String getAccountType = UserProfileService.getAccountTypeByEmail(email);你的方法getAccountTypeByEmail(String email)只返回一个结果,为什么你循环显示它。