0

Im having trouble showing what I put in my ArrayList on JSP. The items in the list are taken from a Database in a Java class and then are shown in the JSP. Ive tried using the c:forEach but I still dont get it that much. Im new to JSP and Im trying not to use the scriplets. Thats why I do everything over my Java class. Also so that you got an idea on how it works, the admin chooses to see the whole database of the users, so it chooses on a option radio button tag, goes to a JSP page where it evaluates what the admin wants to see and forwards to the other page where the Arraylist must be shown. I call the function of the List in the JSP page that forwards to the JSP that shows the Database. I dont know if thats the correct way. Thanks!

This is the JAVA class

public List<administration> fullList() throws Exception{

    List<administration> result = new ArrayList<administration>();

    try{
        Class.forName("com.mysql.jdbc.Driver");
        connectMe = DriverManager.getConnection(url+dbName, userNameDB, passwordDB);

        String query = "SELECT  \n" + 
                " ControlAccess.UserName, \n" +
                " ControlAccess.Pass, \n" +
                " Users.First_Name,\n" +
                " Users.Last_Name, \n" +
                " UserInfo.Age, \n" +
                " UserInfo.Country,\n" +
                " UserInfo.Address,\n" +
                " UserInfo.ZipCode,\n" +
                " Sessions.Matrix1,\n" +
                " Sessions.Matrix2,\n" +
                " Sessions.Result,\n" +
                " FilePath.LocationFiles\n" +
                " FROM MatrixUsers.UserInfo \n" +
                " INNER JOIN MatrixUsers.Users\n" +
                " ON UserInfo.idUserInfo = Users.idUsers\n" +
                " INNER JOIN MatrixUsers.ControlAccess \n" +
                " ON ControlAccess.idControlAccess = UserInfo.idUserInfo\n" +
                " INNER JOIN MatrixUsers.Sessions \n" +
                " ON Sessions.idSessions = ControlAccess.idControlAccess\n" +
                " INNER JOIN MatrixUsers.FilePath \n" +
                " ON FilePath.idFilePath = Sessions.idSessions";

        selectUsers = connectMe.prepareStatement(query);
        selectUsers.executeQuery();

        while(results.next()) {

            administration admin = new administration();

            admin.setUserName(results.getString("UserName"));
            admin.setPassword(results.getString("Pass"));
            admin.setFirstname(results.getString("First_Name"));
            admin.setLastname(results.getString("Last_Name"));
            admin.setAge(results.getInt("Age"));
            admin.setCountry(results.getString("Country"));
            admin.setAddress(results.getString("Address"));
            admin.setZipcode(results.getInt("ZipCode"));
            admin.setMatrix1(results.getString("Matrix1"));
            admin.setMatrix2(results.getString("Matrix2"));
            admin.setResult(results.getString("Result"));
            admin.setLocation(results.getString("LocationFiles"));
            result.add(admin);

        }
       results.close();
       connectMe.close();
    }catch(Exception e) {
        e.printStackTrace();
    }

    return result;
}

This is how Im trying to show in the JSP:

<table>
        <c:forEach var="admin" items="${admin.fullList()}">
            <tr>
                <td>${admin.username}" </td>
                <td>${admin.password} </td>
                <td>${admin.firstName} </td>
                <td>${admin.lastName} </td>
                <td>${admin.age} </td>
                <td>${admin.country} </td>
                <td>${admin.address} </td>
                <td>${admin.zipcode} </td>
                <td>${admin.Matrix1} </td>
                <td>${admin.Matrix2} </td>
                <td>${admin.Result} </td>
                <td>${admin.location} </td>
            </tr>


        </c:forEach>

    </table>
6
  • you are returning result but accessing admin !!! Commented Feb 14, 2015 at 17:07
  • So Ive to change admin to result? ALSO I noticed that in the Java Class I had result.next but I didnt assign anything to result, my mistake! Commented Feb 14, 2015 at 17:10
  • still dont get that much what does that mean is it not showing all data? Commented Feb 14, 2015 at 17:15
  • Im sorry, what I mean is that it only shows the header but no table. Also right now Im editing the code and if I put administration.fullList(); in my forwad JSP I get an error saying javax.el.PropertyNotFoundException: Property 'firstName' not found on type matrixcalculator.administration Commented Feb 14, 2015 at 17:18
  • You forgot to assign the result list to the concrete java.sql.ResultSet here selectUsers.executeQuery();. You instead need this results=selectUsers.executeQuery();. I assumed results is a type of java.sql.ResultSet. You seem to have declared java.sql.ResultSet as a class member which is plain wrong. Declare and use Connection, ResultSet and Statement/PreparedStatement in a shortest possible scope (and precisely close them in a finally block). Commented Feb 14, 2015 at 18:12

1 Answer 1

1

plz try like this

<c:if test="${not empty result}">
  <c:forEach var="e" items="${result}" >
     <c:out value="${e.username}"/>
      </c:forEach>


</c:if>

here i am iterating the list and the username by using c:out.

Sign up to request clarification or add additional context in comments.

7 Comments

By errList you mean the variable that the function returns?
For your case it will be result.
Okay but Ive to use the jsp beans so it would be like exameblr beanName.result ?
No your arrylist you have declared as List<administration> result = new ArrayList<administration>(); so the result list will be in administation type.so you need to call the variable name thait is e.username for getting username
Administration is the class name, so it would be just result in the JSP? :S like how does it know what it is?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.