Solved

Inserting multiple rows to MySQL using preparedstatement

Posted on 2004-09-01
11
503 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
  • 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
Technology Partners: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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