[英]JSP for loop with database results
我正在使用JSP申請大學。 與JSP對應的是與數據庫的連接,我必須檢索該信息並將其顯示在下拉列表中,以便用戶可以選擇一個選項來進行注冊。 我可以連接到數據庫,並在下拉列表中輸出信息,但是它僅獲得輸入到數據庫中的第一行。
String[] course_codes = dbclass.SelectRow("SELECT DISTINCT Course_code FROM Stream ;");
%><form action="successtwo.jsp">
<label>Select Course code</label>
<select name="stream">
<%for(String course_code: course_codes){
out.println("<option value="+course_code+">"+course_code+"</option>");
}%>
</select><br>
所以我做了這樣的事情,但我也做了。
String[] course_codes = dbclass.SelectRow("SELECT DISTINCT Course_code FROM Stream ;");
%><form action="successtwo.jsp">
<label>Select Course code</label>
<select name="stream">
<%for(String course_code: course_codes){%>
<option value="<%=course_code%>"><%=course_code%></option>
<%}%>
</select><br>
在數據庫中,有多個Course_code,並且可能會出現相同的Course_code,因此為什么我要使用distinct。 這是SelectRow方法所在的dbClass.java。
public String[] SelectRow(String SQLquery)
{
String Result[];
// Send an SQL query to a database and return the *single column* result in an array of strings
try {// Make connection to database
statementObject = connectionObject.createStatement();
ResultSet statementResult = statementObject.executeQuery(SQLquery); //Should connection be left open?
ResultSetMetaData rsmd = statementResult.getMetaData();
int nrOfColumns = rsmd.getColumnCount();
Result = new String[nrOfColumns];
statementResult.next();
int currentCounter = 0;
while (currentCounter<nrOfColumns) // While there are rows to process
{
// Get the first cell in the current row
Result[currentCounter] = statementResult.getString(currentCounter+1);
currentCounter++;
}
// Close the link to the database when finished
} catch (Exception e) {
System.err.println("Select problems with SQL " + SQLquery);
System.err.println("Select problem is " + e.getMessage());
Result = new String[0]; //Need to setup result array to avoid initialisation error
writeLogSQL(SQLquery + " caused error " + e.getMessage());
}
writeLogSQL(SQLquery + "worked ");
return Result;
} // End SelectRow
有任何想法嗎?
public String[] SelectColumn(String SQLquery)
{
String Result[];
// Send an SQL query to a database and return the *single column* result in an array of strings
try {// Make connection to database
statementObject = connectionObject.createStatement(); //Should connection be left open?
ResultSet statementResult = statementObject.executeQuery(SQLquery);
// Start solution from http://www.coderanch.com/t/303346/JDBC/java/find-number-rows-resultset
int rowcount = 0;
if (statementResult.last()) {
rowcount = statementResult.getRow();
statementResult.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}
// End solution from http://www.coderanch.com/t/303346/JDBC/java/find-number-rows-resultset
Result = new String[rowcount];
int currentCounter = 0;
while (statementResult.next()) // While there are rows to process
{
// Get the first cell in the current row
Result[currentCounter] = statementResult.getString(1);
currentCounter++;
}
// Close the link to the database when finished
} catch (Exception e) {
System.err.println("Select problems with SQL " + SQLquery);
System.err.println("Select problem is " + e.getMessage());
Result = new String[0]; //Need to setup result array to avoid initialisation error
writeLogSQL(SQLquery + " caused error " + e.getMessage());
}
writeLogSQL(SQLquery + "worked ");
return Result;
} // End Select
這個工作...
使用resultSet.next()將resultSet移至下一行。
要遍歷resultSet,您可以使用類似以下的內容
while (rs.next()) {
Result[currentCounter] = statementResult.getString("columnName");
}
這是一個示例http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.