Java.sql.SQLException: before running the result set

I am having a problem getting data from my database MySql

. My code is as follows:

        try {
        Connection con = datasource.getConnection();
        Statement stmt = con.createStatement();

        ResultSet rs;
        rs = stmt.executeQuery("SELECT tittle,date,path " +
                "FROM announcement "+
                "ORDER BY date");
        String tittle=rs.getString("tittle");
        String date=rs.getString("date");
        String text=rs.getString("path");


     if (!rs.isBeforeFirst())
     {
        out.println("<p>No data !</p>");
    }
     else
     {            
        out.println("<table class=\"data\">");
        out.println("<tr ><td class=\"sectionheader\"> tittleς</td><td            class=\"sectionheader\">date</td><td class=\"sectionheader\">text</td></tr>");

        while (rs.next()) {
            String row="";
            row  += "<td><a href=\"\"> "+tittle+ "</td>";
            row  += "<td>" + date + "</td>";
            row +="<td>"+text+"</td>";

            row +="</tr>";
            out.println(row);

        }

      

The error I am getting is "java.sql.SQLException: Before start of result set"

Any tip of what I am doing wrong?

Thanks in advance.

+3


source to share


1 answer


You access the data in the ResultSet object using a cursor. Initially, the cursor points to the first line.

while(rs.next()) {
      String tittle=rs.getString("tittle");
      ....

}

      



Moreover, your query may return multiple rows, in which case you may want to store the results in a collection List<Map<String, String>>

where each entry in the list represents one row from your queries and map store the column name and column value.

+8


source







All Articles