Parsing textfile and creating sql command

Hi, people!

I need to parse values from textfile lines, and make sql for inserting that values into appropriate table. Each line has a form that looks like this one:

IdNum=xxxx;Name=抶x...x?Address=抶x...x?[LocalNum=xxxx;]Type=抶?

where IdNum, Name, Address, LocalNum and Type are name of columns, xxx are values and value in [ ] is optional columnname=value (columnname can have null values).

How can I do that? I cannot find any example that would help me.

[486 byte] By [dalibora] at [2007-11-26 15:52:21]
# 1
>How can I do that?Do what? Parsing input, or the producing the correct SQL?
DrLaszloJamfa at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...
# 2
Well, parsing input is a bigger problem. I'm good with sql but not so good with Java :-)
dalibora at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...
# 3

OK, I managed to parse names of colums and values. Here is the code:

import java.util.*;

public class Strtok {

public static void main(String [] args) {

String str = "IdNum=xxxx;Name=抶x...x?Address=抶x...x?LocalNum=xxxx;Type=抶?";

StringTokenizer token = new StringTokenizer(str , "= ;");

while (token.hasMoreElements())

System.out.println(token.nextElement());

}

}

and output:

IdNum

xxxx

Name

抶x...x?Address

抶x...x?LocalNum

xxxx

Type

抶?

Now, how can I pass that values into insert query

INSERT INTO TableNAme (IDNum, Name, Address, LocalNum, Type)

VALUES (xxxx, 'xx...x', 'xx...x', xxxx,'x' );

?

dalibora at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...
# 4
What if the delimited strings contain '=' or ';' charecters?String str = "IdNum=xxxx;Name=抶x.;..x?Address=抶x.=..x?LocalNum=xxxx;Type=抶?";
DrLaszloJamfa at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...
# 5
Suppose they don't! I just need a code that would pass line that I exactly describe.
dalibora at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...
# 6
Read up on JDBC -- it'll be slightly different depending upon the database you're using.[url= http://java.sun.com/javase/technologies/database/index.jsp]Sun's JDBC page[/url]
kevjavaa at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...
# 7

Ahhhhh! Cross posted.

http://forum.java.sun.com/thread.jspa?threadID=5127555&tstart=0

Please do not crosspost (post your questions in more than one forum at the same time). Thanks

PS answer to question asked in reply 4 in this thread is answered by me in other thread.

cotton.ma at 2007-7-8 22:12:37 > top of Java-index,Java Essentials,New To Java...