I have a scriplet code that connects to database, fetches records, stores in an arraylist and iterates that arraylist for records.
Following is the code to connect and fetch records for arraylist
<%
String connectionURL = "jdbc:mysql://localhost:3306/stc";
ArrayList<String> productname = new ArrayList<String>();
ArrayList<String> productmodel = new ArrayList<String>();
Connection connection = null;
ResultSet rs = null;
try
{
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "");
//Add the data into the database
PreparedStatement pst = connection.prepareStatement("SELECT * FROM tbl_products");
rs= pst.executeQuery();
while(rs.next())
{
productname.add(rs.getString("txtProduct_Name"));
productmodel.add(rs.getString("txtModel_No"));
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
%>
Following code iterats through the arraylist to display records
<select name="productname" id="productname">
<option>Select Name</option>
<%
for(int i=0;i<productname.size();i++)
{
%>
<option value="<%=productname.get(i) %>"><%=productname.get(i) %></option>
<%
}
%>
</select>
I want to achieve all this functionality by using JSTL or JSP standard action. Please help