11 Replies - 2648 Views - Last Post: 06 September 2009 - 02:35 PM Rate Topic: -----

#1 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Help with DatabaseMetaData getTables method

Post icon  Posted 05 September 2009 - 10:51 PM

Pasted below is my method I created to return a String array containing all of the table names in my data source.


public String[] getTables(){
		try{
		   DatabaseMetaData dbMeta=con.getMetaData();
		   String[] tableNames=dbMeta.getTables(null, null, "%", "TABLE");
		}
		catch(Exception e){
			errMsg="Error:"+e.toString();
		}
	}


I am getting an error on the getTables() line as it is saying I have incompatible types, String[] !=String.

There must be something small which I am missing, but I am unable to find something to help me in the java documentation. Any ideas?

This post has been edited by ryanand88: 05 September 2009 - 10:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with DatabaseMetaData getTables method

#2 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Help with DatabaseMetaData getTables method

Posted 05 September 2009 - 11:11 PM

Based off of the error,

getTables(null, null, "%", "TABLE") returns a single String and not an array of Strings.


http://java.sun.com/...va...ang.String[]%29

api says that it returns ResultSet
Was This Post Helpful? 0
  • +
  • -

#3 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with DatabaseMetaData getTables method

Posted 05 September 2009 - 11:39 PM

So to fill an array of table names would you suggest a for loop? I am still not exactly clear on how to proceed.
Was This Post Helpful? 0
  • +
  • -

#4 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Help with DatabaseMetaData getTables method

Posted 05 September 2009 - 11:48 PM

Well to start you need to store it in a ResultSet
ResultSet tableNames=dbMeta.getTables(null, null, "%", "TABLE");



then get the data from that. Take a look at the api and see if you can figure it out.

* How'd I pull off a triple post?

This post has been edited by syfran: 05 September 2009 - 11:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 12:33 AM

Here is what I ended up with. I realize the tableNames array will need to be initialized differently, other than that I believe this will work.


public String[] getTables(){
		try{
		   DatabaseMetaData dbMeta = con.getMetaData();
		   ResultSet result=dbMeta.getTables(null, null, null,new String[]{"TABLE"});
		   int i=1;
		   String tableNames[]=new String [i];
		   while (result.next()){	  
			   tableNames[i]=result.getString("TABLE_NAME");
			   i+=1;
		   }
		   return tableNames;
		   
		}
		catch(Exception e){
			errMsg="Error:"+e.toString();
			return null;
		}
	}


I am assuming there was a much easier way to do this. After searching through api whatever you were hinting at must have eluded me. Do you have any advice on how to do this differently?

This post has been edited by ryanand88: 06 September 2009 - 12:33 AM

Was This Post Helpful? 0
  • +
  • -

#6 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 12:43 AM

No subtle hints here, I was just assuming you would need the api for getting data out of ResultSet

Didn't actually look at it until now. How would this method work for you?
Was This Post Helpful? 0
  • +
  • -

#7 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 09:30 AM

I am not sure how that would work, considering I am trying to retrieve the table names and not the column names.
Was This Post Helpful? 0
  • +
  • -

#8 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 09:54 AM

I think my solution below will work:

public String[] getTables(){
		try{
		   DatabaseMetaData dbMeta = con.getMetaData();
		   ResultSet rsTables=dbMeta.getTables(null, null, null,new String[]{"TABLE"});
		   int i=1;
		   String tableNames[]=new String [100];
		   while (rsTables.next()){
			   tableNames[i]=rsTables.getString("TABLE_NAME");
			   i+=1;
		   }
		   return tableNames;

		}
		catch(Exception e){
			errMsg="Error:"+e.toString();
			return null;
		}
	}


I have to get through the rest of my program so I can test it though. My one problem is that I don't want to have to explicitly state that there are 100 elements in this array.

This post has been edited by ryanand88: 06 September 2009 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#9 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 09:55 AM

try getArray("TABLE_NAME"); and see what it gives out. The api says you are going to be using a column label for both methods.
Was This Post Helpful? 0
  • +
  • -

#10 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 10:04 AM

With the getArray I am getting an error message saying incompatible types, required String[] and found Array. Here is what it looks like now.

public String[] getTables(){
		try{
		   DatabaseMetaData dbMeta = con.getMetaData();
		   ResultSet rsTables=dbMeta.getTables(null, null, null,new String[]{"TABLE"});
		   int i=1;
		   String tableNames[]= rsTables.getArray("TABLE_NAME");
		  // while (rsTables.next()){
			   //tableNames[i]=rsTables.getString("TABLE_NAME");
			   //i+=1;
		  // }
		   return tableNames;

		}
		catch(Exception e){
			errMsg="Error:"+e.toString();
			return null;
		}
	}

Was This Post Helpful? 0
  • +
  • -

#11 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 11:35 AM

Damn sql has to make everything tough. I hate to keep going since you have your answer but the sql result set returns Array which is apparently different than java arrays. According to the api
String tableNames[]= rsTables.getArray("TABLE_NAME").getArray(); will return an object which should be castable to a String[]

This stuff really isn't that hard if you read the api. It's all there.

This post has been edited by syfran: 06 September 2009 - 11:36 AM

Was This Post Helpful? 1
  • +
  • -

#12 ryanand88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with DatabaseMetaData getTables method

Posted 06 September 2009 - 02:35 PM

Here is my new method, but it doesn't seem to be casting properly. I am not receiving an error, but I am not getting anything back when I call getTables.

public String[] getTables(){
		try{
		   DatabaseMetaData dbmeta=con.getMetaData();
		   String []types={"TABLES"};
		   ResultSet tables=dbmeta.getTables(null,null,"%", types);
		   String [] tableNames=(String[]) tables.getArray("TABLE_NAMES").getArray();
		   return tableNames;

		}
		catch(Exception e){
			errMsg="Error:"+e.toString();
			return null;
		}
	}


It seems to be going through the method and returning no tables. Is there a better way to cast the result set to String[]?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1