Statement st=null;
ResultSet rs=null;
Connection con=null;
String sql="select * from employee;";
try{
con= new Database().getMySqlConnection();
st=con.prepareStatement(sql);
rs=st.executeQuery(sql);
while(rs.next())
{
if(rs.getInt("id")!=-1)
{
%>
<tr><td align="center"><%=rs.getInt("id")%></td>
<td align="center"><%=rs.getString("name")%></td>
<td align="center"><%=rs.getInt("salary")%> </td>
<td align="center"><%=rs.getString("Designation") %></td>
}
else
{
out.println("table is empty");
}
}
}
catch (SQLException ex) {
System.out.println(ex.getMessage());
}
In this program I want to show data in table on page, by retrieving from table if not empty, if table is empty then show message or print that table is empty. But it can’t display any message, when there is data in table it works correctly.
But when table is empty the error message is not displayed.
I saw many examples on the net, perform wasNull() method , islast(), iffirst() method etc, but still problem persists.
Plz suggest the solution. This program is in java.
your empty table statement should be outside the while statement. Use a flag.
the
next()method keeps moving the cursor forward one row at a time. If you enter the while loop,it means there is something in the table.