0
因此,我正在創建一個在線銀行轉賬部分,用戶可以在其賬戶之間轉賬。我有一個下拉框,它將顯示用戶擁有的帳戶數量。我不確定我是否正確地做到了這一點。這是我到目前爲止的代碼:基於數據庫查詢創建下拉框?
轉移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;
}