J
J
Jackson7502018-10-14 10:07:49
Java
Jackson750, 2018-10-14 10:07:49

How to output sql count to jsp?

dao

@Override
    public List<Book> task3() {


        Session session = this.sessionFactory.getCurrentSession();

//оба запроса работают правильно
//try1
         //  String sql = "SELECT books.genre, COUNT(*) AS counter FROM books  GROUP BY genre";
        //try2
        String sql = "SELECT b.genre as name, count(b.id) as count from Books b group by b.genre";

        SQLQuery query= session.createSQLQuery(sql);
        query.addEntity(Book.class);

        List<Book> authorList1   = query.list();

        return authorList1;
    }

Controller
@RequestMapping(value = "/task3", method = RequestMethod.GET)
    public String task3(Model model){
        model.addAttribute("task3", this.bookService.task3());

        return "task3";
    }

view
<c:if test="${!empty task3}">
        <table class="tg">
            <tr>
                <th width="120">name</th>
                <th width="120">genre</th>
                <th width="120">rating</th>
                <th width="120">published</th>

            </tr>


            <c:forEach items="${task3}" var="author">
                <tr>

                    <td>${author.name}</td>
                    <td>${author.genre}</td>
                    <td>${author.rating}</td>
                    <td>${author.published}</td>

                </tr>
            </c:forEach>
        </table>
    </c:if>

entity
@Entity
@Table(name = "Books")

public class Book {


    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;


    @Column(name = "name")
    private String name;


    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = "published")
    @Temporal(TemporalType.DATE)
    private Date published;


    @Column(name = "genre")
    private String genre;

    @Column(name= "rating")
    private Integer rating;

how to correctly display count in jsp? I don't know what field to output along with genre.
There are options for outputting to jsp via jdbc, but the question is the same, the output of count itself.
With such a launch, the request is swearing at non-fulfillment and what the id does not see.
Need help gentlemen

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question