java 使用java将xml文件数据插入数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5959248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert the xml file data into a database using java?
提问by Dixit Wadhwani
I wanted to insert data from a XML file to a database with Java. This includes creating table, followed by inserting data in that from the XML file.
我想使用 Java 将数据从 XML 文件插入到数据库中。这包括创建表,然后从 XML 文件中插入数据。
回答by MByD
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 hereand another one here.
回答by zdmc
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).
如果这是一次性操作和/或您有大量数据,那么您可以考虑绕过 Java (ala JDBC) 中的数据库通信,相反,您可以将输出流式传输到包含有效数据的纯文本 SQL 脚本(插入)SQL 语句。一旦你有了 SQL 脚本,你就可以简单地在本地访问你的数据库(通过命令提示符/行)并导入你新创建的 SQL 脚本。
To parse the XML, please refer to the URL provided by MByDin his point #1.
要解析 XML,请参考MByD在他的第 1 点中提供的 URL 。
回答by user748105
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).
如果这是一次性操作和/或您有大量数据,那么您可以考虑绕过 Java (ala JDBC) 中的数据库通信,相反,您可以将输出流式传输到包含有效数据的纯文本 SQL 脚本(插入)SQL 语句。一旦你有了 SQL 脚本,你就可以简单地在本地访问你的数据库(通过命令提示符/行)并导入你新创建的 SQL 脚本。
To parse the XML, please refer to the URL provided by MByD in his point #1.
要解析 XML,请参考 MByD 在他的第 1 点中提供的 URL。