0

I want to convert the records in the JDBC resultset into insert queries for some purposes.

Is it possible? If it is pls suggest me the solution.

prince
  • 601
  • 6
  • 15
  • 30
  • have you checked http://raresql.com/2014/07/11/sql-server-how-to-convert-select-statement-result-set-into-insert-statements/ – singhakash Feb 18 '15 at 10:05
  • 1
    Am a bit confused here regarding exactly what your objective is. However, I believe you can create insert queries using the `JDBC ResultSet`. In this case, you will have to create stored procedures and pass the values of the `ResultSet` as parameters to the procedure and then execute. If this is not what you intend doing, please elaborate further on your question. – Michael Woyo Feb 18 '15 at 10:05

3 Answers3

1

Here is a question about generating CREATE TABLE query from ResultSet. How to create table based on JDBC Result Set

With some changes you should be able to adapt it for an INSERT query also.

Note that, the values have to be added with quotes or with date conversion function etc. as per the data type of the column.

Also, large objects such as CLOB would require some additional bit of work to make them work.

Community
  • 1
  • 1
Teddy
  • 4,009
  • 2
  • 33
  • 55
0

You can use pure SQL for this.

As MySQL docs says:

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

You can use a select subquery with an insert in other SQL dialects also.

adambene
  • 1,143
  • 9
  • 16
0

Based on my understanding on your quesstion, You can try like this

try{

        ps=con.prepareStatement("select * from login");
                     rs=ps.executeQuery();
                     while(rs.next())
                     {
                          ps=con.prepareStatement("insert into table2(uname,pswd) values(?,?)");
                          ps.setString(1,rs.getString(2));// 2-column number
                          ps.setString(2,rs.getString(3));//3-column number
                          ps.executeUpdate();
                     }



    }catch(Exception e)
    {
        System.out.println(e);
    }
Anptk
  • 1,125
  • 2
  • 17
  • 28