Deepa 13
Deepa 13

Reputation: 57

how to get next database string in java?

I have stored some multi-term strings like [john smith, mary jones, mary john albert,..] in database one by one.

  1. how to get next, next string from database? [already i inserted some strings in DB, again i need to use those string from db and i retrived using stringtokenizer [st1m], then i need check each multi-term string, one by one]
  2. how to check how many times "john" occurs in the database? [here john is occured 2 times in multi-term string]
  3. what is the use of v[j].trim?

my code is:

stas1.executeUpdate("insert into c_ngram values('"+v[j]+"')");

            }
             StringTokenizer st1m=new StringTokenizer(v[j],"/");

              int i1=0;
              String posm1[]=new String[10000];
            while(st1m.hasMoreElements())
            {
               posm1[i1]=st1m.nextToken().replace("/", "").trim();

              il++;
              int ikm=0;
              while(st1m.next()!=null)
                         {

                          //===========;

                               ikm++;

                         }

here v[j] is the strings in database. please help me. thanks in advance.

Upvotes: 1

Views: 87

Answers (1)

I'll answer using the same order:

When you are trying to iterate in a string array of strings in a single string, like "One,Two,Three", you can do this in two forms. The first form is using the method split(","), this method will transform you String in a String[] then you can use a loop to iterate between the elements of your String array:

String x = "One,two,three";
String[] array = x.split(",");
for(String item: array){
  System.out.println(item);
}

You can use the StringTokenizer and use iterate between the elements.

StringTokenizer tokens=new StringTokenizer(x,",");
//then you do this iterator
while(tokens.hasMoreElements()){
  System.out.println(tokens.nextElement());
}

For your second question, lets consider you are inserting a name for each element. Lets use a table for example: person(integer id,varchar name) and you are inserting your names from your string like this example below:

String x = "john smith, mary jones, mary john albert";
StringTokenizer tokens=new StringTokenizer(x,",");
//then you do this iterator
while(tokens.hasMoreElements()){
  ... insert each element.
}

when you want to go to database and locate for example names where contains John use a query using like.

Select p.name from person p where p.name like '%John%';

Using this query the database will try to find in every element and column name the text John.

The third answer the .trim() method removes spaces before the first character and after the last character.

String name = "   Ana Maria    ";
System.out.println(name.trim());

The response will be Ana Maria;

Look more at

About String: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

About StringTokenizer: http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

About QUERY LIKE operators: http://www.w3schools.com/sql/sql_like.asp

Upvotes: 1

Related Questions