How to use sql LIKE clause in java to retrieve records in database?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to use sql LIKE clause in java to retrieve records in database?

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    How to use sql LIKE clause in java to retrieve records in database?

    Hi,I am having difficulties in retrieving data from database when i am using the LIKE clause in my sql statement.What I actually want is when the user enters a name in the search function,names that are similar to the user input will be retrieve out from the database.But unfortunately,I keep retrieving null.My code looks some thing like this.

    public class MemberDetails{

    String name;

    public MemberDetails(String name){
    this.name = name;
    }

    public ArrayList<String> getMemberSearchResults() {
    ArrayList<String> hi = new ArrayList<String>();
    ResultSet rs = null;
    DBController db = new DBController();
    db.setUp("IT2297_Project");
    String dbQuery = "SELECT alumni_Name FROM AlumniMembers WHERE alumni_Name
    LIKE '" + name + "'";
    dbQuery += " Order by alumni_Name";
    rs = db.readRequest(dbQuery);
    try {
    while (rs.next()) {
    name = rs.getString("alumni_Name");
    hi.add(name);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(dbQuery);
    db.terminate();
    return hi;
    }

    public static void main(String args[]) {
    MemberDetails md = new MemberDetails("p");
    System.out.println(md.getMemberSearchResults());
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: How to use sql LIKE clause in java to retrieve records in database?

    The LIKE clause is executed by the DBM and has nothing to do with Java. If your SQL statement not returning what you expect first of all change the SQL to do an exact match for a known alumni name to see if the surrounding code is working. If that returns the correct record see if the SQL with the LIKE statement will return an exact match.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: How to use sql LIKE clause in java to retrieve records in database?

    The LIKE clause probably requires a wildcard of some kind:
    Code:
    where alumni_name like 'Dav%'
    would return all names where the first three characters are Dav.

    Or are you trying to use some sort of phonetic matching (e.g. keyed "Smith" also returns "Smythe" etc)?
    Oracle (and I believe MS SQL server) has a built in function called soundex to perform this - or there are free implementations of this and other algorithms such as metaphone or double metaphone out there on the web.

  4. #4
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: How to use sql LIKE clause in java to retrieve records in database?

    Or you might like to make it case insensitive:
    Code:
    where upper(alumni_name) like 'DAV%'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center