Rakesh
Rakesh

Reputation: 594

Jsp Table printed in wrong way

I have the below code in my jsp. Here in the output the rows are shown as columns and columns are shown as rows.

enter image description here

<%@page import="com.sun.crypto.provider.RSACipher"%>  
<%@include file="DBCon.jsp" %>  
<body><form action="Export1.jsp" method="post">  
<table border="1px"><TR>  
<%  
    ps=con.prepareStatement("Select * from Scope2");  
    rs=ps.executeQuery();  
    ResultSetMetaData rsmd=rs.getMetaData();  
    int NumOfCol=rsmd.getColumnCount();  
for(int i=1;i<=NumOfCol;i++)  
{%></tr>  

<%  
try  
               {  
    ps=con.prepareStatement("Select * from Scope2");  
    rs=ps.executeQuery();  
       while(rs.next()){  

           %>  
                  <td><%=rs.getString(i)%></td>  
           <%  
       }  

}  
catch(Exception e)  
               {  
    out.println(e);  
}}%>  
   </table><table><tr>  
    <input type="Submit" value="Export"></tr></table></form></body> 

and the output is as in Screen 1 but i want the output to be as in screen 2. please help me .

Thanks.

Upvotes: 0

Views: 214

Answers (2)

Mohsin
Mohsin

Reputation: 852

You have messed with the row formatting and the loops. Here is the updated code -

<body><form action="Export1.jsp" method="post">  
<table border="1px">  
<%  
    ps=con.prepareStatement("Select * from Scope2");  
    rs=ps.executeQuery();  
    ResultSetMetaData rsmd=rs.getMetaData();  
    int NumOfCol=rsmd.getColumnCount();  

while(rs.next())  
{%>
<TR>
<%  
try  
     {  
       for(int i=1;i<=NumOfCol;i++){  

           %>  
                  <td><%=rs.getString(i)%></td>  
           <%  
       }  

}  
catch(Exception e)  
{  
    out.println(e);  
}
%></TR><%}%>  

</table><table><tr>  
<input type="Submit" value="Export"></tr></table></form></body>

Hope that helps.

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15664

You forgot the </tr> tag at the end of each your for loop. So that ay be creating formatting problem.

And why are you executing that query again in the for loop while you can use the same ResultSet rs that you have obtained earlier ?

Upvotes: 1

Related Questions