I wanted to insert data from a XML file to a d开发者_Go百科atabase with Java. This includes creating table, followed by inserting data in that from the XML file.
I'm not going to supply you with the code to do it, but give you the direction:
There are two parts to your task:
Parsing the xml - can be done by one of the many XML parsers to Java. refer to this question.
Communicating with the database - can be done using JDBC, which has a nice tutorial here and another one here.
If this is a one time operation and/or you have LOTS of data, then you could consider bypassing the DB communication in Java (ala JDBC), and instead, you could stream the output to plain-text SQL script(s) containing valid (insert) SQL statements. Once you have the SQL script(s), then you could simply access your DB locally (via the command-prompt/line) and import your newly created SQL script(s).
To parse the XML, please refer to the URL provided by MByD in his point #1.
I'm not going to supply you with the code to do it, but give you the direction:
If this is a one time operation and/or you have LOTS of data, then you could consider bypassing the DB communication in Java (ala JDBC), and instead, you could stream the output to plain-text SQL script(s) containing valid (insert) SQL statements. Once you have the SQL script(s), then you could simply access your DB locally (via the command-prompt/line) and import your newly created SQL script(s).
To parse the XML, please refer to the URL provided by MByD in his point #1.
精彩评论