Peetkovski
Peetkovski

Reputation: 48

Creating dynamic table in JSP

I'm trying to do electronic grade book in Java. I want to insert values of Marks and Description into rows

How it looks without description

<%
                  

try{

connection = DriverManager.getConnection(connectionUrl, userid, password);

statement=connection.createStatement();


String sql ="select Marks from Pitew_Matematyka";

 resultSet = statement.executeQuery(sql);

while(resultSet.next()){
    
            %>

<tr>

<td>

<%=resultSet.getString("Marks") %>

    </td>

<td></td>
<td></td>
<td></td>
<td></td>

<% }

connection.close();
} catch (Exception e) {
e.printStackTrace();
}

      %>



       </tr>

But when I try to add Description to row, it looks like this:

after adding description

Is there any way I can also add description to the 3rd row?

<%
                  

try{

connection = DriverManager.getConnection(connectionUrl, userid, password);

statement=connection.createStatement();


String sql ="select Marks from Pitew_Matematyka";

 resultSet = statement.executeQuery(sql);

while(resultSet.next()){


    
     %>

<tr>

<td>

<%=resultSet.getString("Marks") %>

     </td>

<td>

 <% sql ="select Description from Pitew_Matematyka";   %>

 

 <%=resultSet.getString("Description") %>
 
 </td>


<td></td>
<td></td>
<td></td>

<%}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>

 </tr>

I tried many solutions and any of the solution I tried didn't work.

Upvotes: 2

Views: 369

Answers (2)

Khin Me Me Latt
Khin Me Me Latt

Reputation: 126

You should modify the sql query. Please try the below code.

<%
                  

try{

    connection = DriverManager.getConnection(connectionUrl, userid, password);

    statement=connection.createStatement();


    String sql ="select Marks, Description from Pitew_Matematyka";

    resultSet = statement.executeQuery(sql);

    while(resultSet.next()){

%>
<tr>

  <td>

    <%=resultSet.getString("Marks") %>

  </td>
  <td></td>
  <td>
    <%=resultSet.getString("Description") %>
  </td>
  <td></td>
  <td></td>
</tr>
<%
 }
  connection.close();
  } catch (Exception e) {
    e.printStackTrace();
  }

%>

Upvotes: 2

Roman C
Roman C

Reputation: 1

Modify sql string to include a Descriptioncolumn in the result set.

String sql ="select * from Pitew_Matematyka";

When you use sql for retrieving data from the table then using SELECT statement to define column names of the table. If you use * then all columns will be included.

The list of select_expr terms comprises the select list that indicates which columns to retrieve. Terms specify a column or expression or can use *-shorthand:

  • A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.

You can't modify the output by just changing a variable sql which is used for the query. After modifying a query you need to execute it again to get a new result set. And you shouldn't do it because you don't need to execute any queries to get additional columns. But the first query must be changed.

Upvotes: 2

Related Questions