?
Solved

Inserting multiple rows to MySQL using preparedstatement

Posted on 2004-09-01
11
Medium Priority
?
516 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 600 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

801 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