2016-11-15 1 views
0

So erstelle ich eine Online-Überweisung, wo der Benutzer Geld zwischen ihrem Konto überweisen kann. Ich habe ein Dropdown-Feld, in dem die Anzahl der Konten angezeigt wird, die der Benutzer besitzt. Ich bin mir nicht sicher, ob ich das richtig mache. Hier ist mein Code so weit:Erstellen Dropdown-Box basierend auf Datenbankabfrage?

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

Datenbank Klasse:

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; 
     } 

Antwort

0

Ich weiß nicht, unter welchen Umständen Sie in der Drop-Down-Box zeigt erscheinen die Höhe des Kontos Ein Konto, das mehr als eine Bankkarte bindet? Zeichenfolge getAccountType = UserProfileService.getAccountTypeByEmail (E-Mail); Ihre Methode getAccountTypeByEmail (String email) liefert nur ein Ergebnis, weshalb Sie es in der Schleife anzeigen.