Solved

Inserting multiple rows to MySQL using preparedstatement

Posted on 2004-09-01
11
504 Views
Last Modified: 2010-04-01
I have set of 43 rows which should be inserted to mysql
How could I insert them all simultaneously into DB using preparedStatement?
I know that looping could be one solution or are there any better ways to do this?

Like

SQL_Query = "INSERT INTO options VALUES('','number1');
editStatement = connection.prepareStatement(SQL_Query);
editStatement.executeUpdate();

SQL_Query = "INSERT INTO options VALUES('','number2');
editStatement = connection.prepareStatement(SQL_Query);
editStatement.executeUpdate();
0
Comment
Question by:Taxiu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11950661
use a batch update
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 11950663
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11950694
And a PreparedStatement...

PreparedStatement ps = connection.prepareStatement( "INSERT INTO options VALUES( ?, ? )" ) ;
for( int i = 0 ; i < 100 ; i++ )
{
    ps.setString( 1, "" ) ;
    ps.setString( 2, "number" + i ) ;
    ps.addBatch() ;
}
ps.executeBatch() ;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 11950699
Obviously,

for( int i = 0 ; i < 100 ; i++ )

should be:

for( int i = 0 ; i < 43 ; i++ )

and

    ps.setString( 2, "number" + i ) ;

should be

    ps.setString( 2, "number" + ( i + 1 ) ) ;

to accuratley fit your example ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11950702
Hmmmm...spelling "accurately" wrong...  never a good sign ;-)
0
 

Author Comment

by:Taxiu
ID: 11950733
LOL

I'll test those and give points then accordingly.
0
 

Author Comment

by:Taxiu
ID: 11950829
Actually I made that example way too easy what comparing what the real situation is.

I have 43 rows, which each having different text&numbers so each row is unique. I my point of view this changes the code quite dramatically.

True example:
INSERT INTO css VALUES ('','A:link','link color','color','black','2','');
INSERT INTO css VALUES ('','A:link','link size','font-size','10','2','pt');
INSERT INTO css VALUES ('','table.image','','height','14','2','pt');
etc..

I raised points to 200
0
 
LVL 35

Accepted Solution

by:
TimYates earned 150 total points
ID: 11950899
Do this then:

PreparedStatement ps = connection.prepareStatement( "INSERT INTO css VALUES( ?, ?, ?, ?, ?, ?, ? )" ) ;
ps.setString( 1, "" ) ;
ps.setString( 2, "A:link" ) ;
ps.setString( 3, "link color" ) ;
ps.setString( 4, "color" ) ;
ps.setString( 5, "black" ) ;
ps.setString( 6, "2" ) ;
ps.setString( 7, "" ) ;
ps.addBatch() ;

ps.setString( 1, "" ) ;
ps.setString( 2, "A:link" ) ;
ps.setString( 3, "link size" ) ;
ps.setString( 4, "font-size" ) ;
ps.setString( 5, "10" ) ;
ps.setString( 6, "2" ) ;
ps.setString( 7, "" ) ;
ps.addBatch() ;

... all the rest ...

ps.executeBatch() ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11950924
Or, you could pass them through as a double array of strings;

public void addStuff( String[][] values )
{
    PreparedStatement ps = connection.prepareStatement( "INSERT INTO css VALUES( ?, ?, ?, ?, ?, ?, ? )" ) ;
    for( int i = 0 ; i < values.length ; i++ )
    {
        for( int j = 0 ; j < values[ i ].length ; j++ )
        {
            ps.setString( j + 1, values[ i ][ j ] ) ;
        }
        ps.addBatch() ;
    }
    ps.executeBatch() ;
}

and call it with:

String[][] values = { { "", "A:link", "link color", "color", "black", "2", "" },
                               { "", "A:link", "link size", "font-size", "10", "2", "pt" },
                               { "", "table.image", "", "height", "14", "2", "pt" } } ;
addStuff( values ) ;

0
 

Author Comment

by:Taxiu
ID: 11951258
Got it work now.

Solution was to use batch and create table to db from which I fetch rows to batch new insert :)

      Connection MM_connection2 = DriverManager.getConnection MM_editConnection,MM_editUserName,MM_editPassword);
      PreparedStatement ps = MM_connection2.prepareStatement( "INSERT INTO css VALUES( ?,?,?,?,?,?,? )" ) ;
   
      Connection MM_connection3 = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);
      PreparedStatement editStatement = MM_connection3.prepareStatement("SELECT * FROM css WHERE service_id='0'");
      ResultSet rs3 = editStatement.executeQuery();
      while (rs3.next()) {
            ps.setString( 1,"" );
            ps.setString( 2,""+rs3.getString("name"));
            ps.setString( 3,""+rs3.getString("desc"));
            ps.setString( 4,""+rs3.getString("property"));
            ps.setString( 5,""+rs3.getString("value"));
            ps.setString( 6,""+result);
            ps.setString( 7,""+rs3.getString("type"));
            ps.addBatch();
      }

      ps.executeBatch();
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11951303
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
A big percent of today’s marketing activity is performed through the online environment. The marketing strategies that have existed a decade ago no longer relate to what’s happening today. We’re currently facing a revolutionary era, called the digit…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question