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

    Default while loop not executing

    Hi,

    Java Code:
     java.sql.Statement sta = conn.createStatement();
                ResultSet rs2 = sta.executeQuery("SELECT OBJECT_NAME FROM "+GetSrcString+"."+GetSchema+".OPTIM_NAA_CONTROLS");
                 
                 
                while (rs2.next()){
                  
                RecName=rs2.getString("OBJECT_NAME"); 
                
            
               String sqlText = "CREATE TABLE "+GetArcSchema+"."+RecName+" (";
                java.sql.Statement stat = conn.createStatement();
    
                ResultSet rs= stat.executeQuery("SELECT  COLUMN_NAME,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,DATA_TYPE FROM    "+GetSrcString+"."+"INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"+RecName+"' AND      TABLE_SCHEMA ='"+GetArcSchema+"' ORDER BY ORDINAL_POSITION ASC");          
              
               while(rs.next()) {
                    
                   String ColumnName =rs.getString("COLUMN_NAME");
                   System.out.println(ColumnName);
                   String DataType =rs.getString("DATA_TYPE");
                   String ColumnDefault =rs.getString("COLUMN_DEFAULT");
                   String CharaterMaximumLength = rs.getString("CHARACTER_MAXIMUM_LENGTH"); 
                   
                       sqlText = sqlText + ColumnName+" " + DataType + " "+ColumnDefault+",";
                    System.out.println(sqlText);
                    
                   
                }
                
                sqlText = sqlText.substring(0, sqlText.length()-1);
                sqlText = sqlText + ")";
                System.out.println(sqlText);
               boolean  rs1  = sta.execute(sqlText);
                
                }
                
                conn.close();
    As shown in the code i have two while loops, first while is executing where as my inner while loop not executing at all, any reasons why or else how to handle them?

    Regards
    Sandeep
    Last edited by sandeep43; 07-26-2011 at 02:34 PM.

  2. #2
    Amit20 is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Are you getting any errror?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    As written, the second while loop is *not* an inner loop of the first as you suggest. If this loop does not execute then presumably the result set in question is empty.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Learn to format code in a readable fashion.
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  5. #5
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Hi pbrockway2,

    Sorry, the first while brace had to close after second while.

    Amit i am not getting any error but my second while loop is not executing

    Regards
    Sandeep

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

    Default

    Hi DarrylBurke,

    i think now it is readable and can you help me out as why my second while is not executing ?

    Regards
    Sandeep

  7. #7
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Are you sure your query is returning results?

  8. #8
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Hi yellowledbet,

    it is not returning results

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by sandeep43 View Post
    it is not returning results
    Do you see why, if the query does not return any results, the second for loop will not do anything?

    What you need to do is execute a query that does return some results.

    You could try seeing what the query is:

    Java Code:
    String queryStr = "SELECT  COLUMN_NAME,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,DATA_TYPE FROM    "
            +GetSrcString+"."+"INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"
            +RecName+"' AND      TABLE_SCHEMA ='"+GetArcSchema+"' ORDER BY ORDINAL_POSITION ASC");
    [color=green]System. out.println("About to execute query: " + queryStr);[/color]
    ResultSet rs= stat.executeQuery(queryStr);          
    while(rs.next()) {
    You can then check that you are making the query you think you are. And you can check that query with your database using some other interface (command line etc).
    Last edited by pbrockway2; 07-27-2011 at 06:45 AM.

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

    Default

    Can I suggest that the second Statement is placed outside of the first while loop and turned into a PreparedStatement. This is a perfect place for one. Decalre once and reuse each time round the first loop. That won't solve your query not returning anything, but it will improve your code a bit.

Similar Threads

  1. Executing a jar file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 02-14-2014, 10:02 AM
  2. for loop not executing
    By kirtichopra2003 in forum Advanced Java
    Replies: 9
    Last Post: 09-25-2009, 10:22 AM
  3. Executing .exe from java
    By rp181 in forum Advanced Java
    Replies: 6
    Last Post: 09-21-2009, 04:32 PM
  4. Batch Executing
    By sokolramaj in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2009, 05:17 PM
  5. Problem with executing
    By mcal in forum New To Java
    Replies: 2
    Last Post: 02-09-2008, 02:51 PM

Posting Permissions

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