Solved

Inserting multiple rows to MySQL using preparedstatement

Posted on 2004-09-01
11
502 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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