How to get and iterate questions in ember from database

I have written this code in JSP but I wanted the html codes to be in emberJS for that I wanted to send the ResultSet to emberJS and iterate the questions in emberJS. I am newbie to emberJS. Can anyone help me to get through this? Thanks in advance :slight_smile:

Below is my JSP code

<%@page import="dao.MainDao" %>
<%@page import="java.sql.ResultSet" %>

<title>Delete Question</title>

<h1>Online Feedback System</h1>
<h2>Delete Question</h2>

<table align="center" cellpadding="5" cellspacing="5" border="1">
<tr>

</tr>
<tr>
<td><b>Q ID</b></td>
<td><b>QUESTION</b></td>

</tr>

<% 
ResultSet resultSet = MainDao.displayQuestions();
while(resultSet.next())
{
%>
<tr>
<td><%=resultSet.getString("qid") %></td>
<td><%=resultSet.getString("questionmessage") %></td>
</tr>
<% 
}

%>

</table><br><br>

<form>
Enter the Question ID to delete : <input type="text" name="qid" >
<input type="submit" value="submit">
</form>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>


<script>

$(document).ready(function(){
    $("form").on("submit", function(event){
        event.preventDefault();
 
        var formValues= $(this).serialize();
 
        $.post("deletequestionx.jsp", formValues, function(data){
            location.reload();

        });
    });
});

</script> 

If you haven’t already done it, the biggest piece of work here is exposing your data on an API endpoint. Same as we discussed in How to connect mySQL and emberJS framework to send and retrieve data.

To load and display the list of questions, the simplest solution is to load the data from your route:

// app/routes/my-page.js
import Route from '@ember/routing/route';
export default class extends Route {
  async model() {
    let response = await fetch('/your-questions-endpoint-here');
    let json = await response.json();
    return json;
  }
}

And then display it in the template like:

{{!-- app/templates/my-page.hbs --}}
{{#each @model as |question|}}
<tr>
  <td>{{question.qid}}</td>
  <td>{{question.questionmessage}}</td>
</tr>
{{/each}}
1 Like

Hi, Edward. Yes, I am serving it on apache tomcat sever at http://localhost:8080/onlinefeedbacksystemFinal/deletequestion.jsp As in the above JSP code I have iterated the resultset in java which is the query of the all the questions and question ids using JDBC. I guess I cannot return the java resultset to the ember. Is there any other way ? (Now my question is how to send the resultset which contains the questions and question ids to route)

I think what you’re missing is that you don’t want JSP to render HTML. You want it to render something that’s easier for your code to parse and consume – probably JSON. My sample code above assumes JSON. Where I wrote /your-questions-endpoint-here, that would be your http://localhost:8080/onlinefeedbacksystemFinal/deletequestion.jsp

1 Like

Yes I am working on it, Thanks for considering my question :smiley: