title: MySql Database keyword searching with like operator
body: <p>I wanted to search some keywords from particular column and print all the column values corresponding to the keyword, Now the problem is if I use query </p>
  
  <p>Select * from Candidate where skills like %C% , and different rows in skills are:</p>
  
  <p>Java, C, C++, HTML, CSS, Net Beans, My Eclipse</p>
  
  <p>Java, Asp.Net, Eclipse</p>
  
  <p>C, PHP, CSS, Hibernate</p>
  
  <p>Now it is diplaying all the rows as C is a substring in every row, I tried to use C% rather than %C% but only 3rd line is displayed as it starts with C but I need to show all the Rowrs having "C" as a different word, How can I search the full world, Suggest piece of code.</p>
  
  <pre><code>import java.util.*;
  import java.sql.*;
  import java.io.*;
  
  public class TokensOfString 
  {
      public static void main(String[] args) 
      {
          String str;
          String query = "";
          try
          {
              int i =0, cntToken =0;
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              System.out.println("Enter the keywords to be searched");
              str = br.readLine();
              StringTokenizer words = new StringTokenizer(str, ",");
              cntToken = words.countTokens();
              //System.out.println("No. of Tokens = "+cntToken);
  
              String token[]= new String[cntToken];
  
              while(words.hasMoreElements())
              {
                  token[i] = words.nextToken().trim();
                  //System.out.println(token[i]);
                  i++;
              }
                  //System.out.println(++i+" : "+words.nextElement().toString().trim());    
  
              query = "SELECT * from can_skills where skills  like '"+ token[0] +"%'";
             // System.out.println("Query at 34 : "+query);
              if(cntToken == 1)
              {
                  query ="";
                  query = "SELECT * from can_skills where skills  like '"+ str +"%'";
              }
              if(cntToken &gt; 1)
              {
                  for( int j=1; j &lt; cntToken; j++)
                  query = query + " or skills  like '"+ token[j] +"%'";
              }
              System.out.println("Query at 43 : "+query);
  
              Connection con = null;
              Class.forName("com.mysql.jdbc.Driver");
              String hostName = "localhost";
              String port = "3306";
              String userName = "root";
              String password = "root";
              con = DriverManager.getConnection("jdbc:mysql://" + hostName + ":" + port + "/prem", userName, password);
              Statement statement= con.createStatement();
              ResultSet rs = statement.executeQuery(query);
              while(rs.next())
              {
                  System.out.println(rs.getString(4));
              }
  
          }
          catch(ClassNotFoundException e){ System.out.println(e); }
          catch(Exception ex){ System.out.println(ex); }
      }
  }
  </code></pre>