-1

I have a java program which was working fine earlier is giving strange error now.I am not sure of the reason. The flow is such that table name is passed to this function and column names are fetched from other function. Then sql is formed and run to generate the data in HTML table. This is one function from whole java program. java version used is 1.6(can't upgrade to 1.7 as 1.6 is the standard used in all projects). As i checked on google, if you fetch same column multiple times from result set then this error comes but in my case i am fetching the column only once.

public static String GetData(String TableName) throws IOException, SQLException
    {
    Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
    c = DriverManager.getConnection( "jdbc:odbc:dbname","username","password" );

    Statement stmnt = c.createStatement();

    try
    {

    String colname= GetColName(stmnt,TableName);

    String colarray[] = colname.split(",");

    htmlheader+="<table border=\"1\">";
    htmlheader+="<caption><b>"+TableName +"</b></caption>";
    htmlheader+="<tr>";

      for (int n=0; n < colarray.length ; n++)
      {
          htmlheader+="<th>"+colarray[n]+"</th>";

      }
      htmlheader+="</tr>";      
    String sqlqry= "Select "+colname + " from "+TableName +";" ;

    ResultSet result = stmnt.executeQuery(sqlqry);

    while (result.next() )
        {               htmlheader+="<tr>";

          for (int n=0; n < colarray.length ; n++)
          {
              htmlheader+="<td>"+result.getString(colarray[n])+"</td>";**//Exception is coming in this line**

          }
          htmlheader+="</tr>";

        }
      htmlheader+="</table>";
      } 

    catch( Exception e )
    {
        e.printStackTrace();
    }
}

Error is

java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7138)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3907)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5698)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:354)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:411)

Please suggest.
Exception is coming on this line

htmlheader+="<td>"+result.getString(colarray[n])+"</td>"
user2223335
  • 211
  • 2
  • 7
  • 20
  • where is this exception thrown from? – Scary Wombat Nov 11 '13 at 07:02
  • @hyde : As i said(i checked google and old post), this error comes when you fetch one column again from result set but i am fetching each column once only.Also, question is updated to display the error line – user2223335 Nov 11 '13 at 07:06
  • Ok. Sometimes direct SO search may be better than Google, have you checked these: http://stackoverflow.com/search?q=java.sql.SQLException+No+data+found – hyde Nov 11 '13 at 07:10
  • 1
    Also please have a look at SQL Injection. Your code is vulnerable to it. http://technet.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx – Pratik Shelar Nov 11 '13 at 07:12
  • @hyde This does not seem to be related. In the link, the same columns is being retrieved twice. – Scary Wombat Nov 11 '13 at 07:13
  • 1
    @user2223335 - what is the value of `n` when this exception happens. Have you tried debugging this? – Scary Wombat Nov 11 '13 at 07:14
  • Another thing, (at least some) readers are much more likely to look at your code, if you fix the indentation. Stack Overflow does not deal with TAB characters very well, avoid them when pasting code... – hyde Nov 11 '13 at 07:15
  • @user2310289: value of n is 0. Also, surprisingly, i just tried result.getString("NAME") and it is working. when i populated colarray with 0, it is displaying NAME but it is not working when i pass this colarray(n) inside result.getstring – user2223335 Nov 11 '13 at 07:16
  • please put your method GetColName as well in the above code fragment. Also try htmlheader+=""+result.getString(n + 1)+"" instead – Nitin Dandriyal Nov 11 '13 at 07:18
  • Look carefully. Is the String in your array `NAME` or is it `"NAME"`, i.e. the equivalent of the source code `"\"NAME\""`? – Holger Nov 11 '13 at 08:36

1 Answers1

1

Your problem is occurring because you are trying to directly access the result using getString at colarray[n]. Java fails to identify the data you need because the cursor created for accessing data is created in a TYPE_FORWARD_ONLY manner. Change the Statement stmnt = c.createStatement(); to Statement s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

Raptor
  • 61
  • 1
  • 7