java模糊查詢
㈠ java 模糊查詢 求助
你把這個稍微改改就行了,用StringBuilder 拼的sql語句,那樣的話就會實現「果輸入為空就輸出所有」其他的一樣
public static List getString(String name){
String sql = null;
StringBuilder str = new StringBuilder();
str.append("select * from 12.guzhang ");
if(name!=null||name==""){
str.append(" and name like '%"+name+"%'");
}
sql=str.toString();
List carboss = new ArrayList();
Connection con = JDBC.getConnection();
Statement st = null;
ResultSet re = null;
try {
st = con.createStatement();
re = st.executeQuery(sql);
while(re.next()){
carboss.add(re.getInt(1));
carboss.add(re.getString(2));
carboss.add(re.getString(3));
}
return carboss;
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBC.reclose(re, st, con);
}
return null;
}
㈡ 如何在JAVA里做 模糊查詢
可以使用正則表達式實現, 可以字元串本身的方法實現,請看示例:
importjava.util.regex.Pattern;
/**
*@authorArvin
*@time2016/11/821:38
*/
publicclassMain{
publicstaticvoidmain(String[]args){
Stringkeyword=".(你好)";
StringcontentOne="hello.(你好)asd";//LIKE匹配
StringcontentTwo="你好";//LIKE不匹配
//方法一:利用正則表達式
//構造正則表達式
Patternregex=Pattern.compile(keyword);
System.out.println(regex.matcher(contentOne).find());//true
System.out.println(regex.matcher(contentTwo).find());//false
//方法二:利用String的contain方法
System.out.println(contentOne.contains(keyword));//true
System.out.println(contentTwo.contains(keyword));//false
//方法三:利用indexOf方法,找得到說明包含
System.out.println(contentOne.indexOf(keyword)>-1);//true
System.out.println(contentTwo.indexOf(keyword)>-1);//false
}
}
㈢ java中如何模糊查找
你這個把四個字拆開單獨找不就完了= =
所謂的模糊查找最多也就像sql裡面的like
計算機本身就是精確的。你要模糊就要加入人為判斷這是毋庸置疑的。
㈣ java 中模糊查詢
可以通過拼where條件的方式模糊查詢;
String where = 「1=1」;
if(StringUtils.isBlank(custId)){
where = where+" CUSTID = '"+custID+"'";
}
if(StringUtils.isBlank(custname)){
where = where+" CUSTNAME = '"+custname+"'";
}
。。。。。。。
這只是一種模糊查詢的方法,適用於按不確定的條件進行查詢
㈤ 如何用java實現模糊查詢
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestDemo {
public static void main(String[] args) {
List<String> aList=new ArrayList<String>();
aList.add("abc12de34");
aList.add("abc02de02");
String regex="^[a-zA-Z]{3}02[a-zA-Z]{2}02$";
Iterator<String> it =aList.iterator();
while(it.hasNext()){
String str=it.next();
System.out.println(str+"==");
if(str.matches(regex)){
System.out.println(str);
}
}
}
}
㈥ Java程序中模糊查詢問題
String
sql
=
"select
*
from
table
where
Name
like
'%"+keyword+"%'"
這個是拼接字元串
keyword是變數
可以在類裡面定義成其他字元
String
sql
=
"select
*
from
table
where
Name
like
'%keyword%'";
這個只能是查找keyword不是變數
只是說查找keyword字元串
String
sql
=
"select
*
from
table
where
Name
like
%keyword%";
而這個應該會報錯的
因為必須加引號
String
sql
=
"select
*
from
table
where
Name
like
'_keyword_'";
這個應該也會有問題吧
因為_是匹配單個字元的
㈦ 怎麼用java程序對資料庫進行模糊查詢
jsp中使用輸入框獲取要模糊查詢的查詢條件,提交到後台,假設為:abc;sql語句中,將查詢條件傳入,拼接sql語句,類似於:select * from table_name where column_name like '%abc%'
㈧ java中模糊查詢的問題
用CONTAINS
將 sql+=" and a.ORDER_NAME =? ";
改為 sql+=" and CONTAINS(a.ORDER_NAME, '%' ||? || '%') ";
LIKE 直接在資料庫中查找可以查到所有所需數據,但是會掃描整個表,影響性能。
CONTAINS是基於全文索引進行查詢,查詢結果受全文索引分詞的方法影響,查詢結果會不全。
所以需要精確查詢用LIKE,內容搜索用CONTAINS提高效率!
LZ自己決定。
㈨ java如何實現非同步模糊查詢
用ajax做,進入頁面的時候吧表名放在集合里,在搜索text框發生改變的時候就調用ajax訪問後台,獲取集合裡面有包含value的集合,然後回到頁面列印在text下面。列印成一個ul就行了,記得進入頁面的時候就獲取表名的集合,不用每次調用ajax都去訪問一次資料庫。然後當用戶點擊某個li再去訪問資料庫。
㈩ JAVA方法,SQL語句模糊查詢
這問題很眼熟
也可以這樣:
String sql="select * from ARITCLE where type="+type+" and title like "++" and writer like "+writer+"";
改成
String sql="select * from ARITCLE where type="+type+" and title like '%"++"%' and writer like '%"+writer+"'%";
如果writer 這些參數是用戶輸入而且不經過處理的話
拼接字元串生成查詢語句,會使SQL注入攻擊變得相當容易