Hello Ragini
See I am create Database named as testing and a Table named MyTable.
Inside this table One column exist named NAME contain value like swaraj, raj, rani etc.
When I am search for a name contain the string, it display me in output raj and swaraj.
if I search for ragini, It disply me No record found.
I give the detail code use this if not getting just callme 9986734636, other connect to my system by remote desktop connection by using 123.236.219.54/190
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test1 {
public void search(){
/* Think this string data from your JSP/Html page or CommandLine
* As per your require You retrive the value and store in a "searchstrin"
* Here I am just assign a string value.
*/
String searchstring = "raj";
/*
* How you want search, its depend upon you.
* A Name Stararted with spacific name or char then USE in SQL Query LIKE 'srchstring%'
* if Ended with specific Name then USE LIKE '%srchstring'
* if it contain the specification then USE LIKE '%srchstring%'
*
* As per requirement you change the below query
*/
String srch = "select name from Mytable where name LIKE '%" + searchstring + "%'";
try{
/*
* See Here I am Using MySQL.
* So As per Your Database Change Class.forMame(),
* and DriverManager.getConnection().
*/
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:
mysql://localhost:3306/testing";, "root", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(srch);
if(!rs.next()){
System.out.println("No Record Found");
}else{
/*
* If you think why again i am execute the query?
* Because once using rs.next, It point to next record.
* if any record exist in first row it not consider.
*
*/
rs = stmt.executeQuery(srch);
while(rs.next()){
System.out.println(rs.getString("name"));
}
}
}catch(Exception ex){
System.out.println("Exception");
}
}
public static void main(String s[]){
Test1 t = new Test1();
t.search();
}
}
Other Wise Send your code
Thanks
Rajanikant