Reputation: 57
I have stored some multi-term strings like [john smith, mary jones, mary john albert,..]
in database one by one.
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
Reputation: 155
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