将查询作为executeQuery()的参数传递

时间:2014-03-08 10:03:05

标签: mysql jsp

我开发了一个HTML表单,用户应该从显示的选项中进行选择。这些值在jsp页面中检索如下:

String regd=request.getParameter("course");
String semester=request.getParameter("semester");
String Month=request.getParameter("Month"); 
String Year=request.getParameter("Year"); 
String type_1=request.getParameter("type");
String regulation=request.getParameter("regulation");

当任何值不等于“all”时,应将一个子字符串添加到查询中。具体如下:

String count_query="SELECT COUNT(*) FROM `student_details`,`semester_regd_students` WHERE `semester_regd_students`.`regd_no`=`student_details`.`regd_no` ";
String select_query="SELECT * FROM `student_details`,`semester_regd_students` WHERE `semester_regd_students`.`regd_no`=`student_details`.`regd_no` ";
if(!regd.equals("all"))
             query=query+" and `Dept_Id`='"+regd+"'";
if(!semester.equals("all")) 
             query=query+" and `Semester`='"+semester+"'";
if(!Month.equals("all")) 
             query=query+" and `Month`='"+Month+"'";
if(!Year.equals("all")) 
             query=query+" and `Year`='"+Year+"'";
if(!type_1.equals("all")) 
             query=query+" and `type`='"+type_1+"'";
if(!regulation.equals("all")) 
             query=query+" and `Regulation`='"+regulation+"'";

count_query=count_query+query;

当我尝试执行上述查询时,使用以下方法

ResultSet rs_count_query=S.executeQuery(count_query);
if(rs_count_query.getInt("COUNT(*)")<=0)
{
    out.println("We have no records for the input data!!! Please go back and verify . . .");
    out.println("Please make sure that, you have no pending verifications . . .");
}

我有一个例外,它是

org.apache.jasper.JasperException: An exception occurred processing JSP page /Exam Cell/data_dept.jsp at line 33

   30:      count_query=count_query+query;
   31:      out.println(count_query);
   32:      ResultSet rs_count_query=S.executeQuery(count_query);
   33:      if(rs_count_query.getInt("COUNT(*)")<=0)
   34:      {
   35:          out.println("We have no records for the input data!!!                   Please go back and verify . . .");
   36:          out.println("Please make sure that, you have no pending              verifications . . .");.

将参数传递给executeQuery会出现什么问题?

1 个答案:

答案 0 :(得分:0)

根据错误堆栈跟踪,错误在33行:

32:      ResultSet rs_count_query=S.executeQuery(count_query);
33:      if(rs_count_query.getInt("COUNT(*)")<=0)

当生成ResultSet时,其行位置状态将在开始之前 这意味着,未准备好来读取行。您可以通过调用next()来检查它是否可以遍历行。它返回boolean并指示是否有任何行要读取,并在 true 时将行位置移动到 next 可用行。

在您的情况下,您没有调用next()方法,因此会抛出SQLException消息Before start of result set

更改您的代码如下:
示例

ResultSet rs_count_query=S.executeQuery(count_query);
if( rs_count_query.next() && rs_count_query.getInt( "COUNT(*)" ) <= 0 )
{
    out.println("We have no rec...... ...");
    out.println("Please make sure tha..... . . .");
}

或者,如果您正在执行单行返回语句,则可以使用absolute将光标移动到该特定行以进行读取。

示例

ResultSet rs_count_query=S.executeQuery(count_query);
rs_count_query.absolute( 1 );
if( rs_count_query.getInt( "COUNT(*)" ) <= 0 )
{
    out.println("We have no rec...... ...");
    out.println("Please make sure tha..... . . .");
}

<强>建议
当在表达式上没有定义别名时,读取列时,使用列索引号是更好的选择。

getInt( "count(*)" )更改为:getInt( 1 )

Count始终返回0或位置整数。您无需检查是否返回负值。

更改:getInt( "count(*)" ) <= 0
致:getInt( "count(*)" ) == 0
或者:getInt( 1 ) == 0

请参阅

  1. boolean next()
    • 将光标从当前位置向前移动一行。 ResultSet游标最初位于第一行之前;对方法的第一次调用使得第一行成为当前行;第二个调用使第二行成为当前行,依此类推。
  2. boolean absolute( int row )
    • 将光标移动到此ResultSet对象中的给定行号。