Solved

Inserting multiple rows to MySQL using preparedstatement

Posted on 2004-09-01
11
500 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now