JDBC Column not found Exception thrown at runtime

Tags: ,



I try to execute this:

<%String getComments = "select * from comments_tb where car_id = "
                                + request.getParameter("id") + "" order by time_stamp DESC";
                        KarimDatabase karim2 = new KarimDatabase();
                        Statement stm2 = karim2.getCon().createStatement();
                        ResultSet rs2 = stm2.executeQuery(query);
                        while (rs2.next()) {
                            out.println("<p>" + rs2.getString("comment_desc") + "</p>");
                            out.println("<p>" + rs2.getString("time_stamp") + "</p>");

                        }
                        karim2.getCon().close();

                    %>

but i get this result from my tomcat server logs:

java.sql.SQLException: Column 'comment_desc' not found.
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)...

Please take note that I have inspected by database tables and the comment_desc is a column. In fact I try another column’s output string and I get a result.

Answer

If you only need those two columns you could do something like this:

<%String getComments = "select comment_desc,time_stamp from comments_tb where car_id = "
                                + request.getParameter("id") + "" order by time_stamp DESC";
                        KarimDatabase karim2 = new KarimDatabase();
                        Statement stm2 = karim2.getCon().createStatement();
                        ResultSet rs2 = stm2.executeQuery(query);
                        while (rs2.next()) {
                            out.println("<p>" + rs2.getString(1) + "</p>"); //get first column result
                            out.println("<p>" + rs2.getString(2) + "</p>"); //2nd

                        }
                        karim2.getCon().close();

                    %>

Try and see if that works. In regards to your error, doesn’t really make much sense why you would get that unless the column doesn’t exist or maybe because it’s not a string?



Source: stackoverflow