假设用户输入02522作为密码,应显示所有匹配结果以及其他密码,如02521,02520等。也应显示。
我能够显示输入的准确结果,但无法显示其他匹配或附近的结果
环境:Java Struts2。
以下是我的代码:
package org.entity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class SearchBasedonBBAction extends ActionSupport {
BloodBank search2;
SearchBasedonBB search;
List<UpdateStock> BBList;
List<UpdateStock> BBList2;
public SearchBasedonBBAction() {
}
public SearchBasedonBB getSearch() {
return search;
}
public void setSearch(SearchBasedonBB search) {
this.search = search;
}
public List<UpdateStock> getBBList() {
return BBList;
}
public void setBBList(List<UpdateStock> bBList) {
BBList = bBList;
}
public BloodBank getSearch2() {
return search2;
}
public void setSearch2(BloodBank search2) {
this.search2 = search2;
}
public List<UpdateStock> getBBList2() {
return BBList2;
}
public void setBBList2(List<UpdateStock> bBList2) {
BBList2 = bBList2;
}
@Override
public String execute() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
System.out.println("Driver Loaded");
Statement st = con.createStatement();
PreparedStatement ps = con
.prepareStatement("select * from stockinfo where stdcode=?");
ps.setString(1, search.getBbb());
BBList = new ArrayList<UpdateStock>();
BBList2 = new ArrayList<UpdateStock>();
ResultSet rs = ps.executeQuery();
while (rs.next()) {
BBList.add(new UpdateStock(rs.getString("name"), rs
.getString("city"), rs.getString("address"), rs
.getString("contact"), rs.getString("email"), rs
.getString("apositive"), rs.getString("anegative"), rs
.getString("bpositive"), rs.getString("bnegative"), rs
.getString("abpositive"), rs.getString("abnegative"),
rs.getString("opositive"), rs.getString("onegative")));
}
System.out.println("step2");
PreparedStatement ps1 = con
.prepareStatement("select * from stockinfo where stdcode like "
+ search.getBbb() + "");
System.out.println("step3");
ResultSet rs1 = ps1.executeQuery();
while (rs1.next()) {
BBList2.add(new UpdateStock(rs.getString("name"), rs
.getString("city"), rs.getString("address"), rs
.getString("contact"), rs.getString("email"), rs
.getString("apositive"), rs.getString("anegative"), rs
.getString("bpositive"), rs.getString("bnegative"), rs
.getString("abpositive"), rs.getString("abnegative"),
rs.getString("opositive"), rs.getString("onegative")));
}
System.out.println("Ye b hogya na baa...");
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@Override
public void validate() {
// TODO Auto-generated method stub
super.validate();
if (search.getBbb().isEmpty()) {
addFieldError("search.bbb", "Please enter Pin Code");
}
}
}
参见我在S.O.P.撰写的测试句子 程序被执行,但查询的名称不会给出任何输出,也没有错误 当我在搜索框中输入时,我得到02522的输出,但是没有显示像02520,02521这样的其他pincode结果。
有两个部分,一个给出精确的一个和另一个给出任何匹配的结果。如第一部分,如果用户输入02522结果,则显示完全匹配,并在其他部分显示02521,02523的结果。
**注意:stdcode在这里表示密码(在DB中定义为stdcode)**
答案 0 :(得分:1)
当您说&#34;附近的结果时,&#34;我不确定你的意思。你说02522的输入应该匹配02521和02520.SQL中的LIKE
不会这样做。 LIKE
将像这样使用:
WHERE username LIKE '%hut%'
这将匹配我的用户名,或任何用户名与&#34; hut&#34;在它。
这整个部分需要修复:
PreparedStatement ps1 = con
.prepareStatement("select * from stockinfo where stdcode like "
+ search.getBbb() + "");
因为LIKE
并不代表&#34;大概匹配&#34 ;;它具有更具体的含义,与字符串匹配有关。
如果您希望它在一定范围内匹配,则应将其转换为整数并使用BETWEEN
。你没有定义&#34; near&#34;意味着,所以我们无法帮助您,直到您更加具体。
答案 1 :(得分:0)
我认为您可以存储确切的PIN码值,然后在运算符之间使用这个值
SELECT * FROM table WHERE pin BETWEEN variable AND variable + 5