Inserting Queries into a RDBMS from a Java Application
If we would like to insert 10,000 rows of data into a开发者_JAVA技巧 RDBMS from our Java application, I assume a simple way to do so would be to use a while loop.... however surley this is infact rather sending 10,000 inserts of one row as opposed to one insert of 10,000 rows.
I guess I am looking for a way to generate the one insert without having to manually write out 10,000 lines of code.
Thanks in advance!
It's a typical case where you want to use prepared statement. Sending the SQL once parametericed to the RDMS and than filling batches with data…
Turn auto-commit off in the JDBC connection, and call commit on a regular basis.
If your RDBMS supports transactions, you can do it in one pass.
if what you want is to generate the inserts then you could use a scripting language to generate the inserts using data from a text file. I had use groovy to do that and it was very simple. If you want to know the most eficient way to execute the inserts you probably want to use jdbc executeBatch for that. Example
精彩评论