Results 1 to 6 of 6
  1. #1
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default how to call multiple sql statements in java

    Hi,

    I have to execute a sql statement, based on that result i have to executed i have to fetch another data using select statement, based on the second sql i have to execute create statement, and i have thousands of rows of data to handle, how to accomplish that?

    basically the flow is Select --> Select --> create Statements.


    Regards
    Sandeep

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Java Code:
    Connection conn = DriverManager.getConnection(<path>,<username>,<password>);
    
    Statement s = conn.prepareStatement("Select * from <tablename>");
    
    ResultSet rs = s.executeQuery();

    so for multiple selects you would just need to replace values in prepareStatament. Or use a String variable and change its values after each executeQuery in prepareStatement.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    And before you do that, make sure you can't do this as a single SQL.
    Or at least merge the two SELECTs.

    If all you;re doing is:
    Java Code:
    SELECT x FROM y WHERE (some criteria);
    if (x == somethingOrOther) {
        SELECT a FROM b WHERE c = x;
        INSERT INTO n (someColumn) VALUES (a);
    }
    because that there should be a single SQL statement, not 3.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Just nest your JDBC calls. Keep in mind that only one ResultSet associated with a Statement can be open at the same time.
    Lesson: JDBC Basics (The Java™ Tutorials > JDBC(TM) Database Access)

  5. #5
    ontonio is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    This should help. JDBC 2.0 allows you to use the addBatch method, but mysql will not allow multiple queries unles you set that security (allowMultiQueries)property. I hope this helps!
    Last edited by DarrylBurke; 07-31-2011 at 03:47 PM. Reason: Spammy signature links removed

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Unlikely to be able to use batching, since the later SQL statements are dependent on the previous ones.

Similar Threads

  1. if statements for multiple variables
    By dookie1293 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 09:30 AM
  2. How to call set of sql statements in java?
    By sandeep43 in forum JDBC
    Replies: 5
    Last Post: 05-31-2011, 06:58 AM
  3. How to call set of sql statements in java?
    By sandeep43 in forum New To Java
    Replies: 0
    Last Post: 05-30-2011, 06:37 AM
  4. Replies: 4
    Last Post: 12-08-2010, 08:40 AM
  5. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •