急急急急急急急急.......................写一个jsp中的根据条件查询
根据两个条件查询
只有第一个可以查出结果
第二个查不出
两个一起也查不出
程序如下
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" import="javax.swing.JOptionPane" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数据处理</title>
</head>
<body>
<%
 String section=request.getParameter("section_no");
String group=request.getParameter("group");
int  section_no= -100;
boolean flag = false;
if(section!=null && !section.equals(""))
  {section_no = Integer.parseInt(section);
   flag = true;
  }
 String guiz[]=new String[500];
String bianm[]=new String[500];
String mingc[]=new String[500];
String weis[]=new String[500];
String shuie[]=new String[500];
String hans[]=new String[500];
String cux[]=new String[500];
 Connection conn=null;
 Statement Stmt=null;
 ResultSet rs=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@194.1.1.236:1521:css";
String user="stdba";
String password="glq73hzz";
conn=DriverManager.getConnection(url,user,password);
Stmt=conn.createStatement();
String strquery="select e.group_no as group_no,e.name as name,sum(round(b.sales_amnt_td,2)) as sales_amnt_td,  round(sum(round(b.sales_amnt_td_p,2)-round(b.sales_amnt_td_c,2)),2)  as sales_amnt_td_p, round(sum(round(b.sales_amnt_td_c,2)),2)  as sales_amnt_td_c,sum(round(b.prof_amnt_td,2)) as prof_amnt_td,e.section_no from items a,items_sales b,items_recpt c,vat d,store_section_groups e,vat f where a.item_no=b.item_no(+)and a.item_no=c.item_no(+) and a.sell_vat=d.vat_no(+) and a.buy_vat=f.vat_no and a.group_no=e.group_no and a.section_no=e.section_no and a.status>0 and (b.sales_amnt_td<>0 or a.stock_adj<>0 or c.recpt_td<>0 or a.start_stock<>0) ";
if(flag)
{strquery = strquery.concat("and e.section_no='"+section+"'");
 if(group!=null)
  strquery = strquery.concat(" and e.group_no='"+group+"'");
}
else
if(group!=null){
strquery =  strquery.concat("and  e.group_no='"+group+"'");}
else  {strquery = strquery.concat("and e.section_no='"+section+"'");}
strquery =  strquery.concat(" group by e.group_no,e.name,e.section_no");
try{
rs=Stmt.executeQuery(strquery);
}
catch(Exception e)
{
 System.out.print(e+"数据库连接失败!");

int i=0;
%>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr align="center">
<td width="3%" rowspan="3">&nbsp;</td>
    <td width="18%" rowspan="3">柜组</td>
    <td colspan="2" rowspan="2">柜台</td>
    <td height="29" colspan="4">销售金额</td>
    </tr>
  <tr>
  <td height="29" colspan="3" align="center">正常</td>
    <td width="13%" rowspan="2" align="center">促销</td>
    </tr>
  <tr>
  <td width="15%" height="37" align="center">编码</td>
  <td width="15%" align="center">名称</td>
  <td width="15%" align="center">weis</td>
  <td width="12%" align="center">税额</td>
  <td width="12%" align="center">含税</td>
  </tr>
  <% if(rs!=null)
     if(rs.next())
      { do
         { 
          guiz[i]=rs.getString(7);
          bianm[i]=rs.getString(1);
          mingc[i]=rs.getString(2);
          weis[i]=rs.getString(3);
          shuie[i]=rs.getString(4);
          hans[i]=rs.getString(5);
          cux[i]=rs.getString(6);
   %>
  <tr>
    <td>&nbsp;</td>
    <td><%=guiz[i]%></td> 
    <td><%=bianm[i]%></td>
    <td><%=mingc[i]%></td>
    <td><%=weis[i]%></td>
    <td bgcolor="#FFFFE1"><%=shuie[i]%></td>
    <td bgcolor="#FFFFE1"><%=hans[i]%></td>
    <td bgcolor="#DFF9FF"><%=cux[i]%></td>
  </tr>
   
<%
     i++;
  }while(rs.next());
 }
%>
</table>
</body>
</html>
<% 
      System.out.println("database link sucess!");
      Stmt.close();
      conn.close();
 %>帮帮忙!先谢了

解决方案 »

  1.   

    代码好乱,看的眼睛累,拜托格式化了再贴好不好。
    另外,你可以把你的SQL直接在数据库里执行看看有没有结果?
      

  2.   

    没看到你的条件在什么地方,在sql语句中?
      

  3.   

    if(flag)
    {strquery = strquery.concat("and e.section_no='"+section+"'");
    if(group!=null)
    strquery = strquery.concat(" and e.group_no='"+group+"'");
    }
    else
    if(group!=null){
    strquery =  strquery.concat("and  e.group_no='"+group+"'");}
    else  {strquery = strquery.concat("and e.section_no='"+section+"'");}
    strquery =  strquery.concat(" group by e.group_no,e.name,e.section_no"); 
      

  4.   

    sql和数据库连接都是没问题的
      

  5.   

    那个GROUP是不是中文的?如果是的话需要解决乱码问题
      

  6.   

    条件不对吧,看看flag和group的值是什么