?
Solved

method getColumnName

Posted on 2006-05-11
5
Medium Priority
?
273 Views
Last Modified: 2012-05-05
Hi everyone,
I have the problem in java programming that I can't get the return in getColumnName method. Can you help me figure out what wrong with that.

Here is my work:

  public String getColumnName(int numColumns) throws Exception
      {
                   Connection dbConn = DriverManager.getConnection("jdbc:odbc:BMAC");
                  Statement stmt = dbConn.createStatement();
                  ResultSet rs = stmt.executeQuery("SELECT a, b, c, d FROM Table");
                 ResultSetMetaData rsmd = rs.getMetaData();
                  for (int i=1; i<numColumns+1; i++)
                  {
                  String columnName = rsmd.getColumnName(i);
                  }
                  return columnName;

     }
0
Comment
Question by:westerntour
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16662270
This would seem more logical:

  public String getColumnName(int numColumns) throws Exception
      {
                Connection dbConn = DriverManager.getConnection("jdbc:odbc:BMAC");
               Statement stmt = dbConn.createStatement();
               ResultSet rs = stmt.executeQuery("SELECT a, b, c, d FROM Table");
               ResultSetMetaData rsmd = rs.getMetaData();
               return rsmd.getColumnName(numColumns);
     }


Either way, I'm not clear what the problem is...

Are you getting an exception? If not, what value is the getColumnName() returning?
0
 

Author Comment

by:westerntour
ID: 16662359
Yes, I mean the value return of the getColumnName() method. The return columnName doesn't work.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16662579
..What do you mean by "doesn't work" ?

Returns null ? .. what ?
0
 

Author Comment

by:westerntour
ID: 16662868
It doesn't accept return String columnName
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 1000 total points
ID: 16662882
Oh yes.

The method I posted above should fix that.

But what the problem is with your code, is that the columnName variable is declared within a different scope to the return statement.. so as far as the return statement is concerned, the columnName variable doesnt' exist.

Change it to:

 public String getColumnName(int numColumns) throws Exception
      {
                Connection dbConn = DriverManager.getConnection("jdbc:odbc:BMAC");
               Statement stmt = dbConn.createStatement();
               ResultSet rs = stmt.executeQuery("SELECT a, b, c, d FROM Table");
               ResultSetMetaData rsmd = rs.getMetaData();
               String columnName = null ;
               for (int i=1; i<numColumns+1; i++)
               {
               columnName = rsmd.getColumnName(i);
               }
               return columnName;

     }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Starting up a Project

807 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