[JSP] Search function

因為JSP我係自學, 可能講得唔清楚, 希望大家體諒

咁我依家個JSP網係連住database, 我依家想整個search function, 例如輸入學生個電話號碼, 就可以show出黎。依家問題黎啦, 我依家用下呢句:
<%if(studentcontactnum.equals(contactnum.get(i))){%>
        <!-- Show student detail -->
<%}
就一定要學生個電話號碼同輸入搜查個號碼完成吻合, 先show到student detail, 但我係想做s1.contains(s2) [Java]呢個效果, 即係只需部分吻合, 都可以show到出黎。
例如database有以下3個學生資料:
student        contactnum
A                12345678
B                45678901
C                13579246

當我search "456"時, 會SHOW到student A同B嘅detail出黎


如果我問得唔清楚, 希望可以教一教我






有嘗試過嫁下咁, 但唔work
<%if(studentcontactnum.contains(contactnum.get(i))){%>
        <!-- Show student detail -->
<%}

唔work係因為主僕調轉左....?
你明唔明contains點解?

TOP

本帖最後由 drupalspring 於 2013-6-14 01:13 編輯

why don't use the "LIKE" condition in the SQL where clause to directly retrieve the records back to JSP ?

Btw , I don't like to embed any Java codes inside JSP directly  , it is the a bad practice IMO.

TOP

回復 2# 神秘二代


    你意思係咁?
<%if(contactnum.get(i).contains(studentcontactnum)){%>
        <!-- Show student detail -->
<%}
但係都唔work....


依家咁,係用黎判斷字串中(contactnum.get(i))是否包含 studentcontactnum?
                                       ^                                         ^
                              form databases                    form user input

TOP

回復  神秘二代


    你意思係咁?
waiyinlwy 發表於 2013-6-14 01:20


好少碰jsp, 但唔知同data type有無關係....(tostring/ parse string...etc)

TOP

if ( contactnum.get(i).indexOf( studentcontactnum ) > -1 ) not recommended

should do it in sql,
select * from TABLE_NAME where contact like '%123%'
all elements in "contactnum" will contain 123

TOP

回復 6# blackwidow


   
    唔該你先,你提到嘅select * form xxx ......
呢句如果我係command prompt度就識用,但如果係一個.jsp嘅網站度做,請問呢句應該放係邊個file度call? 定直接放係.jsp度?

TOP

回復 5# 神秘二代


    如果係java入面,我就知大概點做但依家係jsp網頁

TOP

回復 7# waiyinlwy

i don't know how you build your program, it can be inside jsp or java,
but as you said, you are connecting db with jsp, so i guess you should put inside jsp file
show us more about your jsp script

some ref
http://www.tutorialspoint.com/jsp/jsp_database_access.htm
http://www.9lessons.info/2008/09 ... -mysql-atabase.html
or you are using something like hibernate?

TOP

actually where did you put your sql script?

TOP