0

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

Nishant123
  • 1,968
  • 2
  • 26
  • 44

2 Answers2

0

You can use foreach in jstl to iterate the ArrayList productname like this:

   <c:forEach var='parameter' items='${productname}'>  
      <c:out value="${parameter.name}" /> 
    ...
   </c:forEach><br>

Youdo the same with ither ArrayList productmodel .

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

The above code can be re-factored to use JSTL. The JSTL that may be needed in this case would be the Core, SQL tags.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

For the first code snippet make use of the sql:setDataSource , sql:query tags to connect to the DB and execute query.

 <sql:setDataSource var="demoDB" driver="com.mysql.jdbc.Driver"
         url="jdbc:mysql://localhost/demo"
         user=""  
         password=""/>

<sql:query dataSource="${demoDB}" sql=" " var="queryResult" />
  • sql attribute will contain your query.

Iterate the list using c:forEach tags on the object queryResult.

Please refer to the thread: Java ArrayList using in JSTL(<c:foreach>)

Community
  • 1
  • 1
Ashoka
  • 935
  • 7
  • 20
  • Thanks this is just what i wanted. Can you please tell me how can I put the variable "demoDB" in application scope so that I don't have to establish connection on every page and can just use the variable? – Nishant123 Jan 09 '16 at 10:03