Below is the code which i have written to retrieve values from the database (I have added the whole code so it will be easier for you to understand what i am trying to say here):
package ipscheme;
import java.sql.*;
public class AddRecords {
Connection con = new DBConnection().getConnection();
ResultSet resultSet = null;
public String [] populateSelect() throws SQLException {
Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery("SELECT * FROM ipsections");
resultSet.last();
int size = resultSet.getRow();
resultSet.beforeFirst();
String [] sectionName = new String[size];
int j = 0;
while (resultSet.next()){
sectionName[j] = resultSet.getString("section_name");
j = j + 1;
}
resultSet.close();
statement.close();
con.close();
return sectionName;
}
}
And down here is the JSP Code which i have used to populate values from the database into a selectbox.
<select name="sltSection">
<%
AddRecords added = new AddRecords();
String sectionNm [] = added.populateSelect();
for(int i=0; i<sectionNm.length; i++){ %>
<option>
<% out.print(sectionNm[i]); %>
</option>
<% } %>
</select>
The above code works fine without any compilation errors. As you can see i have used Java code in my .jsp page to fulfill the task, which is a wrong thing to do.
I need to implement this code according to the MVC and therefore i should use JSTL tags to implement the code to .jsp? if so how do i do that? How do i instantiate Object from the class AddRecords
and call its methods? Can someone please help me. Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…