未找到JDBC列在运行时引发异常

问题描述 投票:0回答:1

我尝试执行此操作:

<%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();

                    %>

但是我从tomcat服务器日志中得到此结果:

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

[请注意,我已经检查了数据库表,并且comment_desc是一列。实际上,我尝试另一列的输出字符串,然后得到结果。

java jsp servlets
1个回答
1
投票

如果只需要这两列,则可以执行以下操作:

<%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();

                    %>

请尝试看看是否可行。关于您的错误,除非该列不存在或者不是因为它不是字符串,否则您为什么会得到这个错误并没有太大的意义?

© www.soinside.com 2019 - 2024. All rights reserved.