Results 1 to 8 of 8

Thread: Creating a java bean to execute an sql query.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Creating a java bean to execute an sql query.

    I have an sql database and i'm trying to link to it using a java bean with some sql. I'm having trouble at the moment, i thought i had cracked it with my code beolw, but i'm unable to compile the code. I get the following error for the line that i have made bold below, error:

    webtech/questionaalt.java [63:1] cannot resolve symbol
    symbol : variable i
    location: class webtech.questionaalt
    result += "<td>" + results.getObject(i).toString()+ "</td>";
    ^
    Can anyone help????

    Code:

    Code:
    private Connection dbconn = null;
    public questionaalt()
    {
      try
      {
       Class.forName(driver);
       dbconn = DriverManager.getConnection(url, uname, dpass);
       /*Create an SQL statement*/
       Statement statement = dbconn.createStatement();
       
       if (statement.execute(query))
       {/*Step 3) If we have a result lets loop through*/
       ResultSet           results = statement.getResultSet();
       ResultSetMetaData   metadata = results.getMetaData();
       /*Validate result.  Note switch to while loop if we plan on multiple results from query*/
       if(results != null)
       {
        /*Use results setmetadata object to determine the columns*/
        int li_columns = metadata.getColumnCount();
    
    
    
    
    result = "<tr>";
    for(int i = 1; i <= li_columns;i++)
    {
    result+="<td>" + metadata.getColumnLabel(i) + "</td>";
    }
    result += "</tr>";
    
    
    while (results.next())
    {
    
    result +="<tr>";
    for (int i=1; i <= li_columns; i++);
    {
    result += "<td>" + results.getObject(i).toString()+ "</td>";
    }
    result += "</tr>";
    }
    }
    
    }
    
    }

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: Creating a java bean to execute an sql query.

    Quote Originally Posted by Dilenger4
    for (int i=1; i <= li_columns; i++);
    Hey??
    Isn't that what i already have?

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Creating a java bean to execute an sql query.

    You are delcaring the variable i within the scope of the if() statement. The compiler sees the ; after the if() statement and treats it as one line. The variable i is within a totally different scope or out of the scope where is was declared.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: Creating a java bean to execute an sql query.

    Quote Originally Posted by Dilenger4
    You are delcaring the variable i within the scope of the if() statement. The compiler sees the ; after the if() statement and treats it as one line. The variable i is within a totally different scope or out of the scope where is was declared.
    So how can i resolve it?

  6. #6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: Creating a java bean to execute an sql query.

    am i being stupid??....Where am i puttin the { to relpace the ; ?????? i'll then need a furhter } surely?

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Creating a java bean to execute an sql query.

    Try this.
    Code:
    private Connection dbconn = null;
    public questionaalt(){
      try{
       Class.forName(driver);
       dbconn = DriverManager.getConnection(url, uname, dpass);
       /*Create an SQL statement*/
       Statement statement = dbconn.createStatement();
       
       if (statement.execute(query)){
       ResultSet           results = statement.getResultSet();
       ResultSetMetaData   metadata = results.getMetaData();
       /*Validate result.  Note switch to while loop if we plan on multiple results from query*/
       if(results != null){
        /*Use results setmetadata object to determine the columns*/
        int li_columns = metadata.getColumnCount();
     
       result = "<tr>";
       for(int i = 1; i <= li_columns;i++){
         result+="<td>" + metadata.getColumnLabel(i) + "</td>";
       }
        result += "</tr>";
        while (results.next()){
         result +="<tr>";
         for (int i=1; i <= li_columns; i++){
           result += "<td>" + results.getObject(i).toString()+ "</td>";
          }
           result += "</tr>";
          }
         }
        }
       } 
      }

Posting Permissions

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



Click Here to Expand Forum to Full Width