Insert multiple rows of records into the database

The codes below allow me to insert a row of record into the database. How would I changed these to insert multiple rows at once? Please help!
String sql = "INSERT INTO EMPLOYEES" +
"(First_Name, Last_Name, Title, Phone) " +
" VALUES " +
PreparedStatement statement = conn.prepareStatement(sql);
statement.setObject (1, First_Name);
statement.setObject (2, Last_Name);
statement.setObject (3, Title);
statement.setObject (4, Phone);
boolean returnValue = statement.execute();

Hi mystiqueX,
As wmolosho has suggested in his answer to this very same question that you also posted to the JavaServer Pages forum, you can create a batch of inserts and perform them using the "executeBatch()" method. I will use Craig's sample code to demonstrate:
(Note that this code is untested!)
conn.setAutoCommit(false);
PreparedStatement statement = conn.prepareStatement(sql);
// assume you have an array of objects here
for (int i = 0; i < data.length; i++) {
  statement.setString(1, data<i>.getFirstName());
  statement.setString(2, data<i>.getLastName());
  statement.setString(3, data<i>.getTitle());
  statement.setString(4, data<i>.getPhone());
  statement.addBatch();
statement.executeBatch();
conn.commit();If you are not familiar with it, allow me to suggest looking at the Making Batch Updates lesson on the Java Tutorial.
Hope it helps.
Good Luck,
Avi.

Similar Messages

  • How to speed insert my 1000000 records into the database?

    my code like:
    <cfloop from="1" to="#inserteddb.getrecordcount()#"
    index="x">
    <!----
    Here make the InsertFieldList and InsertValueList
    --->
    <cfquery datasource="#cfdsn#" name="insertdata">
    insert into inputtest (#InsertFieldList#)
    values (
    <cfqueryparam value="#InsertValueList#"
    cfsqltype="cf_sql_varchar" list="yes">
    </cfquery>
    </cfloop>
    The test inserts 100,000 records, has spend I 30 minutes
    time,but I have 1,000,000 record to insert , is there any way to
    enhance the insertion speed?
    Thanks a lot.

    By removing ColdFusion from the process as much as possible.
    Where is the 'insertedDB' data coming from? It looks to be a
    record set?
    Are you moving data from one data source to another? If so,
    some DBMS
    have the ability to insert an entire record set in one step.
    I do not
    have the exact syntax at my finger tips, but I have done
    something like
    this in the past with Oracle. INSERT INTO aTable SELECT FROM
    bTable.
    Are you building a record set from a text file such as CSV?
    If so, many
    DBMS have the ability to do 'bulk' inserts from such text
    files and CF
    does not even need to be involved.
    As you can see, knowing exactly what are you working with
    will help us
    provide suggestions an how to improve your process.

  • Somehow ejbCreate method of an Entity EJB is not inserting a record into the database. I'm using BMP and calling the Entity bean method from a servlet. I'm using NAS 4.0 sp5 on Win 2k.

    Also if I call the insert query from within the servlet it works fine. I have disabled global transactions. Am I missing something out ? Please
    any help would be greatly appreciated.

    May be your servlet is not able to lookup the EJB. You can use some print statements and see whether lookup ok or not.
    Just a guess.
    Plese get back, if any queries.
    Thanks,
    Rakesh.

  • Insert multiple rows using sql at the same time

    I have searched a lot from the forum, but found nothing.
    There are two tables in oracle scheme: TableA(ID_A, column2,column3),
    TableB(ID_B, column2, column3); and TableA has multiply rows already, TableB is empty. TableB's ID_B has a
    Now I want to copy values from TableA.column2 and TableA.column3 to TableB.column2, TableB.column3.
    INSERT INTO SYSTEM_USER_GROUP (ID_user_group,ID_USER,ID_GROUP) values(((SELECT seq_system_user_group.NEXTVAL FROM DUAL),(SELECT U.ID_USER FROM SYSTEM_USER U),(SELECT U.ID_GROUP FROM SYSTEM_USER U)));
    ORA-00907: missing right parenthesis
    Any suggestion will be great appreciate!

    INSERT INTO SYSTEM_USER_GROUP (ID_user_group,ID_USER,ID_GROUP)
    (SELECT seq_system_user_group.NEXTVAL, ID_USER, ID_GROUP,
    FROM SYSTEM_USER)
    ORA-00936: missing expression
    ------------>>>
    I think that seq_system_user_group is a scheme level of oracle, so if you select it from a table, there will not work.
    Thank you all the same!
    Can i create that sequence as a column of one table? if that is the case, then my problem will be resolved.

  • Insert Multiple Rows in JSP

    The codes below allow me to insert a row of record into the database. How would I changed these to insert multiple rows at once? Please help!
    String sql = "INSERT INTO EMPLOYEES" +
    "(First_Name, Last_Name, Title, Phone) " +
    " VALUES " +
    PreparedStatement statement = conn.prepareStatement(sql);
    statement.setObject (1, First_Name);
    statement.setObject (2, Last_Name);
    statement.setObject (3, Title);
    statement.setObject (4, Phone);
    boolean returnValue = statement.execute();

    You can either create a loop and repeat the insert statement,
    or use the addBatch() and executeBatch() methods in the
    java.sql.Statement class.

  • Can you use ESB to insert multiple records into a Database

    We have an XML file that has a Parent/Child relationship.
    In situation 1 we have a single parent record in the xml file. We can insert the record into the database successfully.
    In situation 2 we have a parent and a an associated child record. By using a filter expression to identify the existence of the child record we can route to the first DB Adapter to insert the parent then to the second DB Adapter to insert the child.
    Is this final scenario logical and also possible.
    Situation 3 we have multiple child records associated to a single parent. So we would first insert the parent record then insert all the child records.
    Thanks

    I use PL/SQL for this if you are using Oracle.
    Have a look at this post, it is for AQ but the concept can be used for PL/SQL
    Re: Can I pass a pl/sql table as a parameter from a database adaptor
    I will send you an example
    cheers
    James

  • Inserting Multiple Rows into Database Table using JDBC Adapter - Efficiency

    I need to insert multiple rows into a database table using the JDBC adapter (receiver).
    I understand the traditional way of repeating the statement multiple times, each having its <access> element. However, I am just wondering whether this might be performance-inefficient, as it might insert records one by one.
    Is there a way to ensure that the records are inserted into the table as a block, rather than record-by-record?

    Hi Bhavesh/Kanwaljit,
    If we have multiple ACCESS tags then what happens is that the connection to the database is made only once. But the data is inserted row by row.
    Why i am saying this?
    If we add the following in JDBC Adapter..logSQLStatement = true. Then incase of multiple inserts we can see that there are multiple
    <i>Insert into tablename(EMP_NAME,EMP_ID) VALUES('J','1000')
    Insert into tablename(EMP_NAME,EMP_ID) VALUES('J','2000')</i>
    Doesnt this mean that rows are inserted one by one?
    Correct me if i am wrong.
    This does not mean that the transaction is not guaranted. Either all the rows will be inserted or rolled back.
    Regards,
    Sumit

  • How to insert multiple rows in the database table with the high performance

    Hello everybody,
    I am using the struts,jsp and spring framework. In my application there are 100s of rows i have to insert into the database 1 by 1. I am using usertransaction all other things are working right but i am not getting the real time performance.
    Can anyone tell me the proper method to insert multiple records and also with fast speed

    I don't know much about Spring etc, but if the jdbc Statemenet.addBatch(), Statement.executeBatch() statements let you bundle a whole lot of sql commands into one lump to execute.
    Might help a bit...

  • Insert multiple rows into a same table from a single record

    Hi All,
    I need to insert multiple rows into a same table from a single record. Here is what I am trying to do and I need your expertise. I am using Oracle 11g
    DataFile
    1,"1001,2001,3001,4001"
    2,"1002,2002,3002,4002"
    The data needs to be loaded as
    Field1      Field2
    1               1001
    1               2001
    1               3001
    1               4001
    2               1002
    2               2002
    2               3002
    2               4002
    Thanks

    You could use SQL*Loader to load the data into a staging table with a varray column, then use a SQL insert statement to distribute it to the destination table, as demonstrated below.
    SCOTT@orcl> host type test.dat
    1,"1001,2001,3001,4001"
    2,"1002,2002,3002,4002"
    SCOTT@orcl> host type test.ctl
    load data
    infile test.dat
    into table staging
    fields terminated by ','
    ( field1
    , numbers varray enclosed by '"' and '"' (x))
    SCOTT@orcl> create table staging
      2    (field1  number,
      3     numbers sys.odcinumberlist)
      4  /
    Table created.
    SCOTT@orcl> host sqlldr scott/tiger control=test.ctl log=test.log
    SQL*Loader: Release 11.2.0.1.0 - Production on Wed Dec 18 21:48:09 2013
    Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
    Commit point reached - logical record count 2
    SCOTT@orcl> column numbers format a60
    SCOTT@orcl> select * from staging
      2  /
        FIELD1 NUMBERS
             1 ODCINUMBERLIST(1001, 2001, 3001, 4001)
             2 ODCINUMBERLIST(1002, 2002, 3002, 4002)
    2 rows selected.
    SCOTT@orcl> create table destination
      2    (field1  number,
      3     field2  number)
      4  /
    Table created.
    SCOTT@orcl> insert into destination
      2  select s.field1, t.column_value
      3  from   staging s, table (s.numbers) t
      4  /
    8 rows created.
    SCOTT@orcl> select * from destination
      2  /
        FIELD1     FIELD2
             1       1001
             1       2001
             1       3001
             1       4001
             2       1002
             2       2002
             2       3002
             2       4002
    8 rows selected.

  • Inserting multiple rows into database using c#

    Hi,
    I have a web form with a customer id and a few checkboxes with different products (productid) and when a user checks the checkbox he must also enter a date for that product. 
    I need to basically do the following insert into Customer  (customerid,productid, purchasedate) values (1,890,18/12/2003)
    insert into Customer  (customerid,productid, purchasedate) values (1,56,12/12/2004)
    For each product checked I need to insert the details into the database with the same customerid.
    The database table looks like this:
    Customer Table
    CustomerId int
    ProductId int
    PurchaseDate datetime
    What is the correct way to do that? Should I change the database table?
    Thanks

    This has nothing to do with C#. Ask in the SQL forum for your database product for bulk insertion options.
    Visual C++ MVP

  • To insert multiple rows by getting the values from the user

    Hi
    I want to insert multiple rows into a table using user given values dynamically.
    I am a beginner to PL/SQL. Below code is part of my big code. A_row_insert is 2 and A_count is 1 (initially). While i execute , it asks for input only once and insert the same data 2 times. I want to enter two different rows. Please guide/advise me on this.
    while (A_count<=A_row_insert) loop
    dbms_output.put_line('Enter the value  for the row#'||A_count||' of the table '||v_req_tab_name||':');
    begin
    INSERT INTO customers (
    customer# ,
    lastname ,
    firstname ,
    address ,
    city ,
    state ,
    referred ,
    region
    +)+
    VALUES
    +(+
    +&customer,+
    +'&lastname' ,+
    +'&firstname' ,+
    +'&address' ,+
    +'&city' ,+
    +'&state' ,+
    +&referred ,+
    +'&region'+
    +);+
    end;
    A_count := A_count 1;+
    end loop;
    regards
    Sakthivel

    hi,
    You need to write a procedure and call it from front end for n number of time..... check the parameters you need to pass to execute the procedure.... try given sample....
    CREATE OR REPLACE PROCEDURE InsertData(i_code IN VARCHAR2,i_code_desc IN VARCHAR2,i_code_type IN VARCHAR2)
    AS
    BEGIN
         INSERT INTO CODE_MASTER (code,code_desc,code_type)
    VALUES(i_code,i_code_desc,i_code_type);
    EXCEPTION WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE ( 'Error during Insert code - '||i_code );
    END;
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
    With the Partitioning, OLAP and Data Mining options
    exec InsertData('code1','desc1','type1');
    Thnx MB

  • How to fetch records from the database into a combo box?

    Hi:
    I&acute;m really new with ABLBPM and I&acute;m trying to fetch records from the database to display them into a combo box as valid values for a presentation but I&acute;m using a dynamic method with this code:
    <em>for each row in SELECT campo1, campo2 from TABLE</em>
    <em>do</em>
    <em>solicitudes[] = [row.campo1, row.campo2]</em>
    <em>end</em>
    <em>return solicitudes
    </em>And the debugger says that SQL instructions can be used only in fuctions and procedures that are executed on the server.
    Do you know another way to do it?
    P.D. Sorry for my terrible english
    Greetings

    Hi Steve,
    Thank you, your idea is perfect, but when I try to run the screenflow where the combo should be filled I get this error:
    fuego.lang.ComponentExecutionException: No se ha podido ejecutar correctamente la tarea.
    Motivo: 'java.lang.NullPointerException'.
         at fuego.web.execution.InteractiveExecution.setExecutionError(InteractiveExecution.java:307)
         at fuego.web.execution.InteractiveExecution.process(InteractiveExecution.java:166)
         at fuego.web.execution.impl.WebInteractiveExecution.process(WebInteractiveExecution.java:54)
         at fuego.webdebugger.servlet.DebuggerServlet.redirect(DebuggerServlet.java:136)
         at fuego.webdebugger.servlet.DebuggerServlet.doPost(DebuggerServlet.java:85)
         at fuego.webdebugger.servlet.DebuggerServlet.doGet(DebuggerServlet.java:66)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
         at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
         at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
         at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
         at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
         at fuego.web.execution.servlet.ServletExternalContext.forwardInternal(ServletExternalContext.java:197)
         at fuego.web.execution.servlet.ServletExternalContext.processAction(ServletExternalContext.java:110)
         at fuego.webdebugger.servlet.DebuggerExecution.dispatchComponentExecution(DebuggerExecution.java:64)
         at fuego.web.execution.InteractiveExecution.invokePrepare(InteractiveExecution.java:351)
         at fuego.web.execution.InteractiveExecution.process(InteractiveExecution.java:192)
         at fuego.web.execution.impl.WebInteractiveExecution.process(WebInteractiveExecution.java:54)
         at fuego.web.execution.InteractiveExecution.process(InteractiveExecution.java:223)
         at fuego.webdebugger.servlet.DebuggerServlet.doDebug(DebuggerServlet.java:148)
         at fuego.webdebugger.servlet.DebuggerServlet.doPost(DebuggerServlet.java:82)
         at fuego.webdebugger.servlet.DebuggerServlet.doGet(DebuggerServlet.java:66)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    Any ideas??
    Thanks and greetings

  • Different methods involved in insertion of records into the table

    hi all,
    i am using Forms [32 Bit] Version 6.0.8.24.1 (Production)
    Oracle Database 10g Release 10.2.0.1.0 - Production
    i have parameters like emp_code(optional) and year(mandatory field-four digit year).
    Based on the parameter i have to get the details from one table and those records should be inserted into other table along with additions columns(by hardcoding the additional columns).
    To acheive this i am planning to get a cursor by selecting the necessay fields from the table passing the parameters in the where clause of the cursor.
    by looping through the cursor i will insert the records into the other table.
    here the thing is the data is going to be huge (since year is the parameter).
    so is my approach leads to the performance issue.
    i heard that there are different method of insetions,
    can anybody suggest me the better way(peformance wise) other than what i have mentioned (if you are aware of)..
    Thanks..

    This is not really a Forms issue, so you'd better go to the sql and pl/sql forum. Here, this question has been asked many times: how do I insert large volumes of data in a table?
    insert into ...
    select ....
    or CTAS (create table as select ....)
    by looping through the cursor i will insert the records into the other table.That is really the worst idea. If you can do it in sql (insert into ... select ...) DON'T do it in pl/sql.

  • Air - Sqlite with Adobe Air insert data in memory, but do not record on the database file

    I have the code:
    var statement:SQLStatement = new SQLStatement();
    statement.addEventListener(SQLEvent.RESULT, insertResult);
    statement.addEventListener(SQLErrorEvent.ERROR, insertError);
    statement.sqlConnection = sqlConnection;
    statement.text = "insert into table values('"+TINome.text+"','"+TISerial.text+"') ";
    statement.execute();
    This run without error and the data is inserted (i dont know where), but when i see into database with firefox sqlite manager, the database is empty! But the Air continue run for a time like the data was recorded on the database file. I dont know what is happen with it.
    Help please!

    Toplink In Memory was developed by our project to solve this problem. It allows us to run our test either in memory or against the database.
    In memory, we basically stub out the database. This allows us to speed up our tests about 75x (In memory we run 7600 tests in 200 secs, it takes about 5 hours against the database). However, it throws away things like transactions, so you can't test things like rollback.
    In database mode, it just uses toplink, Another benefit of it though is that it watches all the objects created allowing an automatic cleanup of created test objects - keeping your database clean and preventing test interactions.
    We used HSQL running in memory previously, it worked fine. However, we needed to write scripts to translate our Oracle SQL into HSQL. Also, we had to override things like the data function in HSQL, and a few of our queries behaved unexpectedly in HSQL. We later abandoned it, as it became a lot of maintenance. It was about 10x faster than running against Oracle.
    Interestingly, we install oracle on all our developers machines too, tests run way faster locally, and developers feel way more comfortable experimenting with a local instance than with a shared instance.
    You can find the toplink in memory stuff at:
    http://toplink-in-mem.sourceforge.net/
    I provide all support for it. Doc is sketchy but I'm happy to guide you through stuff and help out where I can with it.
    - ted

  • Inserting multiple rows into a table (using sequences)

    Hi!
    I want to insert multiple rows into a db table, but I don't know how.
    I know how to insert 1 row using a sequence:
    I put all the fields in the jsp form's page and in the submit page I put something like this:
    <jbo:Row id="myRow" datasource="ds" action="Update" rowkeyparam="MyRowKey" >
    <jbo:SetAttribute dataitem="*" />
    </jbo:Row>
    But how can I insert multiple rows like this:
    Id          Name
    1          ana
    2          monteiro
    3          maria
    Thanks!

    Hi Chris,
    I think this should give you what you need: Working with a Multiple Select List Item
    --Jennifer                                                                                                                                                                                                                                                                                                                                                                                                   

Maybe you are looking for

  • Where can I buy Contribute 3?

    Hi, I'm using Dreamweaver 8, and want to buy a compatible copy of Contribute - which I think is Contribute 3. (I assume that later Contribute versions are not compatible). I'd be grateful for information about suppliers that stock Contribute 3. Thank

  • Question mark flashing om screen

    question mark flashing on screen.

  • Burning MP3 disks from purchased iTunes Store songs

    Making MP3 disks from songs I have purchased from the iTunes Store is pretty inconvenient: first I have to make a Music CD, then I have to reimport the files as MP3s, then, finally, I can make an MP3 disk. Is there something I'm missing? I wish it wa

  • Word and PowerPoint - Incorrect Layout

    Has anyone else noticed when viewing Word or PowerPoint documents on the iPhone that the layout of the document (spacing, font size, etc.) doesn't always seem to match the original document like you would view on your computer? It's especially notice

  • Cannot connect to LinkedIN

    Hi, I have the Tour 9630. The internet browser, email and facebook application work. Till very recently App World and Twitter was not working. However, now I cannot connect to: LINKEDIN - "Unable to login: Unable to connect to LinkedIn. Please check