Search code examples
javasqlprepared-statementresultsetbatch-updates

How to insert rows dynamically from a select query to another table in java?


I have a select query (using a prepared statement) which returns a Resultset. For example the query can be :

 SELECT * FROM TABLE;

and I want the result of this query to insert in another table, the select query is stored at database and will be usually a join query, so i don't know exactly the columns of the table, how its possible for each row of the Resultset to make an insert row in the form:

INSERT INTO TABLE VALUES ('THE VALUES FROM THE SELECTED ROW IN THE RESULTSET');

Solution

  • you can do both in one state ment:

    insert into TableB (Col1, col2, col3, col4, etc..)
    select Col1, col2, col3, col4, etc.. from Table A where Col5='condition'