-1

我有 5 个 JSP 页面,用于创建 4 个相关的下拉菜单,并在选择最后一个下拉菜单时显示一个表格,代码如下:第一页:index.jsp

<%@page import="java.sql.*"%>
  <html>
  <head>  
  <script language="javascript" type="text/javascript">  
      var xmlHttp  
      var xmlHttp
  var global
  var state
  var district
      function showState(str){
  global=str;
      if (typeof XMLHttpRequest != "undefined"){
      xmlHttp= new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
  xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlHttp==null){
  alert("Browser does not support XMLHTTP Request")
  return;
  } 
  var url="state.jsp";
  url +="?count=" +str;
  xmlHttp.onreadystatechange = stateChange;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);

  }

  function stateChange(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
  document.getElementById("state").innerHTML=xmlHttp.responseText   
  }   
  }
  function showCity(str){
  if (typeof XMLHttpRequest != "undefined"){
    xmlHttp= new XMLHttpRequest();
    }
  else if (window.ActiveXObject){
    xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
  if (xmlHttp==null){
  alert("Browser does not support XMLHTTP Request")
  return;
  } 
  var url="city.jsp";
  state=str;
  url +="?count=" +str + " " + global;
  xmlHttp.onreadystatechange = stateChange1;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  }
  function stateChange1(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
  document.getElementById("city").innerHTML=xmlHttp.responseText   
  }   
  }
  function showBank(str1){
  if (typeof XMLHttpRequest != "undefined"){
    xmlHttp= new XMLHttpRequest();
    }
  else if (window.ActiveXObject){
    xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
  if (xmlHttp==null){
  alert("Browser does not support XMLHTTP Request")
  return;
  } 
  var url="bank.jsp";
  district=str1;
  url +="?count=" +str1;
  xmlHttp.onreadystatechange = stateChange2;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  }
  function stateChange2(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
  document.getElementById("bank").innerHTML=xmlHttp.responseText   
  }   
  }
  function showPage(str2){
  if (typeof XMLHttpRequest != "undefined"){
    xmlHttp= new XMLHttpRequest();
    }
  else if (window.ActiveXObject){
    xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
  if (xmlHttp==null){
  alert("Browser does not support XMLHTTP Request")
  return;
  } 
  var url="table.jsp";
  url +="?count=" +str2 + " " + state + " " + district;
  xmlHttp.onreadystatechange = stateChange5;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  }
  function stateChange5(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
  document.getElementById("bank").innerHTML=xmlHttp.responseText   
  }   
  }

  </script>  
  </head>  
  <body> 

  <select name='country' onChange="showState(this.value)" style="top:150px; position:absolute;">

   <option value="none">&nbsp;DBTL Phase&nbsp;</option>  

  <%
 java.sql.Connection con;
con=null;
{
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con =     java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  }
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("select DISTINCT DBTLdist from districts ORDER BY DBTLdist ASC");
 while(rs.next()){
 %>
  <option><%=rs.getString(1)%></option>  
  <%
 }
 %>
  </select>  

  <div id='state'>  

  <select name='state' style="left:200px; top:150px; position:absolute;"> 

   <option value="none">&nbsp;&nbsp;&nbsp;States&nbsp;&nbsp;&nbsp;</option> 

  <option value='-1'></option> 

  </select>  
  </div>  

  <div id='city' >  

  <select name='city' style="left:400px; top:150px; position:absolute;">  
   <option value="none">&nbsp;&nbsp;&nbsp;Districts&nbsp;&nbsp;&nbsp;</option>
  <option value='-1'></option>  
  </select>  
  </div>
 <div id='bank' >  

  <select name='bank' style="left:600px; top:150px; position:absolute;">  
   <option value="none">&nbsp;&nbsp;&nbsp;Banks&nbsp;&nbsp;&nbsp;</option>
  <option value='-1'></option>  
  </select>  
  </div>






  </body> 
  </html> 

第二页:state.jsp

<%@page import="java.sql.*"%>

<%
String phase=request.getParameter("count");
out.println(phase);

String buffer="<select name='state' style='left:200px; top:150px; position:absolute;'s     onchange='showCity(this.value);'><option value='-1'>Select</option><option>All</option>";  

try{
if(phase.equals("none"))
{
java.sql.Connection con;
con=null;
{
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  }
 Statement stmt = con.createStatement(); 
  ResultSet rs = stmt.executeQuery("select * from states ORDER BY State_Long ASC ");  
  while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";  
   }  
   }
   else
   {
 java.sql.Connection con;
con=null;
{
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  }
 Statement stmt = con.createStatement(); 
  ResultSet rs = stmt.executeQuery("select * from states where State_Code IN (select     State_Code from districts where DBTLdist='"+phase+"') ORDER BY State_Long ASC ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";  
   }  
  }
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
}
catch(Exception e){
    System.out.println(e);
 }
%>

第三页:city.jsp

<%@page import="java.sql.*"%>
 <%@page import="java.util.*"%>

<%
String state=request.getParameter("count");
StringTokenizer st = new StringTokenizer(state," ");
   String s1=st.nextToken();
      String s2=st.nextToken();

String buffer="<select name='city' style='left:400px; top:150px; position:absolute;' onChange='showBank(this.value);'><option value='-1'>Select</option><option>All</option>";  
try{

  if(s1.equals("All"))
  {
  java.sql.Connection con;
con=null;
{
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con =     java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  }
   Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery(" select * from districts where DBTLdist='"+s2+"' ORDER BY District_Name ASC");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(2)+"</option>";  
   }  
   }
   else if(s2.equals("null") || s2.equals("none"))
   {
   java.sql.Connection con21;
con21=null;
{
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con21 =     java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  }
   Statement stmt21 = con21.createStatement();  
 ResultSet rs21 = stmt21.executeQuery(" select * from districts where State_Code='"+s1+"' ORDER BY District_Name ASC");  
   while(rs21.next()){
   buffer=buffer+"<option value='"+rs21.getString(1)+"'>"+rs21.getString(2)+"    </option>";  
   }
   }
   else
   {
   java.sql.Connection con2;
con2=null;
{
 Class.forName("oracle.jdbc.driver.OracleDriver");
   con2 = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  }
   Statement stmt2 = con2.createStatement();  
 ResultSet rs2 = stmt2.executeQuery(" select * from districts where DBTLdist='"+s2+"' AND State_Code='"+s1+"' ORDER BY District_Name ASC");  
   while(rs2.next()){
    buffer=buffer+"<option value='"+rs2.getString(1)+"'>"+rs2.getString(2)+"</option>";  
   }
   }
  buffer=buffer+"</select>";  
  response.getWriter().println(buffer);


  }
 catch(Exception e){
     System.out.println(e);
 }
  %>

第 4 页:bank.jsp

<%@page import="java.sql.*"%>

<%
String district=request.getParameter("count");
out.println(district);
String buffer="<select name='bank' style='left:600px; top:150px; position:absolute;'      onChange='showPage(this.value);'><option value='-1'>Bank</option><option>All</option>";  

try
{
if(district.equals("All"))
{
java.sql.Connection con1;
con1=null;
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con1 = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 Statement stmt1 = con1.createStatement(); 
  ResultSet rs1 = stmt1.executeQuery("select * from banks ORDER BY bank_name ASC");  
   while(rs1.next()){
   buffer=buffer+"<option value='"+rs1.getString(1)+"'>"+rs1.getString(2)+"</option>";  
   }  
   }
   else
   {
java.sql.Connection con;
 con=null;
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 Statement stmt = con.createStatement(); 
  ResultSet rs = stmt.executeQuery("select DISTINCT * from banks where bank_code IN (select bank_code from bankdetail where district_code='"+district+"') ORDER BY bank_name ASC");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(2)+"</option>";  
    }  
   }
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
}
catch(Exception e){
     System.out.println(e);
 }

%>

第 5 页:table.jsp

<%@ page import="java.sql.*" %>
<%@page import="java.util.*"%>
<%
String bank=request.getParameter("count");

StringTokenizer st = new StringTokenizer(bank," ");
   String bank_code=st.nextToken();
      String state=st.nextToken();
         String district=st.nextToken();
 String buffer="<select name='bank' style='left:600px; top:150px; position:absolute;' onChange='showPage(this.value);'><option value='-1'>Bank</option><option>All</option>";  

   try
    {
    if(district.equals("All"))
{
java.sql.Connection con1;
con1=null;
 Class.forName("oracle.jdbc.driver.OracleDriver");
  con1 =     java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 Statement stmt1 = con1.createStatement(); 
  ResultSet rs1 = stmt1.executeQuery("select * from banks ORDER BY bank_name ASC");  
   while(rs1.next()){
   buffer=buffer+"<option value='"+rs1.getString(1)+"'>"+rs1.getString(2)+"</option>";  
    }  
    }
   else
   {
 java.sql.Connection con;
 con=null;
  Class.forName("oracle.jdbc.driver.OracleDriver");
   con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 Statement stmt = con.createStatement(); 
  ResultSet rs = stmt.executeQuery("select DISTINCT * from banks where bank_code IN (select bank_code from bankdetail where district_code='"+district+"') ORDER BY bank_name ASC");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(2)+"</option>";  
   }  
   }
  buffer=buffer+"</select>";  
  response.getWriter().println(buffer); 
 }
  catch(Exception e){
     System.out.println(e);
 }

%>

<html><body>
 <br><br><br>
<table width="900" border="1" style="top:250px; position:relative; ">
 <th>State</th><th>District</th><th>Bank Name</th><th>Branch Name</th>
 <%
  try
{
if(bank_code.equals("All"))
 {
java.sql.Connection con1;
 con1=null;
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con1 = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 Statement stmt1 = con1.createStatement(); 
  ResultSet rs1 = stmt1.executeQuery("SELECT DISTINCT states.State_Long,districts.District_Name,banks.bank_name,bankdetail.branch_name FROM bankdetail INNER JOIN states ON bankdetail.state_code=states.State_Code AND states.State_Code='"+state+"' INNER JOIN districts ON bankdetail.district_code=districts.District_Code AND districts.District_Code='"+district+"' INNER JOIN banks ON bankdetail.bank_code=banks.bank_code ");  
   while(rs1.next()){
   %>
   <tr style="text-align:center;" >
   <td><%out.print(rs1.getString(1));%></td>
   <td><%out.print(rs1.getString(2));%></td>
   <td><%out.print(rs1.getString(3));%></td>
    <td><%out.print(rs1.getString(4));%></td></tr>
    <%
    }
     }
     else
   {
   java.sql.Connection con1;
 con1=null;
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con1 = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
  Statement stmt1 = con1.createStatement(); 
  ResultSet rs1 = stmt1.executeQuery("SELECT DISTINCT states.State_Long,districts.District_Name,banks.bank_name,bankdetail.branch_name FROM bankdetail INNER JOIN states ON bankdetail.state_code=states.State_Code AND states.State_Code='"+state+"' INNER JOIN districts ON bankdetail.district_code=districts.District_Code AND districts.District_Code='"+district+"' INNER JOIN banks ON bankdetail.bank_code=banks.bank_code AND banks.bank_code='"+bank_code+"'");  
   while(rs1.next()){
   %>
   <tr style="text-align:center;" >
   <td><%out.print(rs1.getString(1));%></td>
    <td><%out.print(rs1.getString(2));%></td>
   <td><%out.print(rs1.getString(3));%></td>
   <td><%out.print(rs1.getString(4));%></td></tr>
    <%
    }
    }
     }
   catch(Exception e){
      System.out.println(e);
 }

%>
</table></body></html>

这些页面显示下拉数据..最后显示银行表的选择..但该表包含列中的重复条目,例如

                   state      district      bank       branch
                  punjab      bathinda      SBI          ABC
                  punjab      bathinda      SBI          PQR
                  punjab      bathinda      SBI          XYZ

但我想消除这种重复……想要这样的桌子……

                   state      district       bank       branch
                   punjab     bathinda       SBI         ABC
                                                         PQR
                                                         XYZ

请帮我这样做..thanku,还可以帮助我简化此代码...作为编码新手

4

1 回答 1

0

您必须跟踪该列的最后一个值并将其与新值进行比较。如果它没有改变,则抑制该值。如果它发生了变化,请显示该值。并在循环结束时将最后一个值设置为新值。

.... 这里是您将其应用于其他列的地方。

 String invalid_value = "some invalid value";
 String col1_old = invalid_value;
 .....
 while(rs1.next())
 {
    out.print("<tr style=\"text-align:center;\">");
    if(!col1_old.equals(rs1.getString(1)))
    {
          out.print("<td>"+rs1.getString(1)+"</td>");
    }
    else
    { 
           out.print("<td>&nbsp;</td>");
    }
    .....
     col1_old = rs1.getString(1);
    .....
    out.print("</tr>");
}
于 2013-10-03T23:21:19.957 回答