我有"公司名称","联系人","电话号码"在我的下拉菜单中。当我选择"联系人"和"公司"并在文本框中输入一些名称,然后按"搜索"按钮它应该显示包含该人名的记录。如果我们不在文本框中输入任何名称并选择"联系人"和"公司"并按"搜索"按钮它应该显示"联系人"的所有记录。如果我选择"电话"和"公司"并在文本框中输入一些数字,它应该显示该记录,否则如果我们不输入任何数字,那么它应该显示电话号码的所有记录。这是我的代码如下:
purchase.jsp
<form action="view.jsp" method="post">
<select name="complan">
<option value="">Make a selection</option>
<option value="Company Name">Company Name</option>
<option value="Contact Person">Contact Person</option>
<option value="Phone Number">Phone Number</option>
</select>
<select name="category">
<option value=""> Make a selection </option>
<option value="company">company</option>
<option value="institution">institution</option>
<option value="hospital">hospital</option>
<option value="Others">Others</option>
</select>
<input type="text" name="search"/>
<input type="submit" value="Submit"/>
</form>
view.jsp的
<form>
<%
String search=request.getParameter("search");
session.setAttribute("sea",search);
String category=request.getParameter("category");
session.setAttribute("cat",category);
String complan = request.getParameter("complan");
session.setAttribute("com",complan);
%>
<select onchange="setAction(this.value)">
<option value=''> Make a selection </option>
<option value='sample.jsp'> PDF</option>
<option value='XLS_LEAD.jsp'> XLS </option>
<option value='DOC_LEAD.jsp'> DOC </option>
<option value='XLSX_LEAD.jsp'> XLSX </option>
</select>
<br/>
<input type="submit" value="Submit">
</form>
sample.jsp
<body>
<%
Connection conn = null;
String sear=(String)session.getAttribute("sea");
String cate=(String)session.getAttribute("cat");
String comp=(String)session.getAttribute("com");
System.out.println("1 is:"+sear);
System.out.println("2 is:"+cate);
System.out.println("3 is:"+comp);
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String sql="select * from lead where Category='" + cate.replaceAll("\\'","''") + "'";
if(sear!=null && sear.trim().length()>0)
{
sql+=" AND Company_Name like '" + sear.replaceAll("\\'","''") + "%'";
}
else if(sear!=null && sear.trim().length()>0)
{
sql+=" AND Contact_Person like '" + sear.replaceAll("\\'","''") + "%'";
}
else
{
sql+=" AND Phone like '" + sear.replaceAll("\\'","''") + "%'";
}
String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
InputStream input = new FileInputStream(new File(jrxmlFile));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("sql",sql);
byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, map, conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
}
catch(Exception e)
{e.printStackTrace();}
%>
</body>
report10.jrxml
<parameter name="sql" class="java.lang.String"/>
<queryString>
<![CDATA[$P!{sql}]]>
</queryString>
答案 0 :(得分:1)
你得到了奇怪的代码:
if(sear!=null && sear.trim().length()>0)
{
sql+=" AND Company_Name like '" + sear.replaceAll("\\'","''") + "%'";
}
else if(sear!=null && sear.trim().length()>0)
{
sql+=" AND Contact_Person like '" + sear.replaceAll("\\'","''") + "%'";
}
如果是,那么它永远不会进入你的其他地方
if (true){//i go here}else if (true){//How can I arrive here if it was false,
hence the else says it needs to be false}
所以在创建你的sql时,你需要根据你需要的语句做你。
如果(我有人和人)我这样做......
如果(我有电话)我这样做ecc。
如果您仍有问题通过评论,请尝试改进您的代码...
由于您通过电子邮件发送了一些代码,我将提供其他解决方法
String sql="select * from lead where Category Like'" + cate.replaceAll("\\'","''") + "%'";
//We check that user has input something in the search box
if(sear!=null && sear.trim().length()>0)
{
boolean isnumber = isNumber(sear); //isNumber(String text) is a metod that you need to create;
//I have not really understand what excatly you like,
//but it could be like this
if (isnumber || complan.equals("Phone Number")){
sql+=" AND Phone like '" + sear.replaceAll("\\'","''") + "%'";
} else if (complan.equals("Contact Person")){
sql+=" AND Contact_Person like '" + sear.replaceAll("\\'","''") + "%'";
}else{
sql+=" AND Company_Name like '" + sear.replaceAll("\\'","''") + "%'";
}
}else{
//we do nothing since we like to show all records
}
在这种情况下
如果输入的是一个号码或选定的电话号码,则会在电话号码栏中搜索。
如果输入不是数字并使用联系人进行搜索,则会在该列上选择
否则搜索公司名称
我不太喜欢这个号码的部分,因为您在选项中有电话号码,请注意,您永远找不到名为1234的公司,因为我们会搜索电话号码...为什么不按用户指示搜索?,如果没有显示,请选择默认值...
如果您不想找到与输入匹配的所有内容,但只需要更改确切的匹配
like '" + sear.replaceAll("\\'","''") + "%'"
到
= '" + sear.replaceAll("\\'","''") + "'"