Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get table name

Status
Not open for further replies.

farnaz

Programmer
Nov 4, 2007
4
AE
hi
I have a problem,I want to get table name from sql query in java

I have used : "ResultSetMetaData md = rs.getMetaData();"

but, "md.getTableName(1)" returns an empty string

furthermore,my driver is"com.microsoft.jdbc.sqlserver.SQLServerDriver"

I have used "ResultSetMetaDataOptions=1" in connection string but this

has not worked.

can anyone help me to solve my problem?
 
Found that code in an old project:
Code:
 // Gets the database metadata
DatabaseMetaData dbmd = connection.getMetaData ();
dbProduct = dbmd.getDatabaseProductName ();
// System.out.println ("product: " + dbProduct);
// Specify the type of object; in this case we want tables
String[] types = {"TABLE"};
ResultSet resultSet = dbmd.getTables (null, null, "%", types);

// Get the table names
while (resultSet.next())
{
	try
	{
		Table t = new Table (resultSet, connection);
		// Oracle system-tables contain a $ in their names, filter them out:
		if (
			(dbProduct.equals ("Oracle") &&  (! t.getName().matches (".*\\$.*"))) ||
		// Postgres system-tables contain pga_ in their names, filter them out:
			(dbProduct.equals ("PostgreSQL") &&  (! t.getName().matches ("pga_.*")))
		)
			sorted.add (t);

don't visit my homepage:
 
Thanks for your reply,

but I want to get spesific table from a sql query not all

tables in database.

for example if my query would be "SELECT Receiver FROM

Cartable Full Join Messaging on Messaging.ID =Cartable.ID",

how could I get "cartable" and " Messaging"?


 
you have got it right ,

I don't know what my query looks like

could you help me?
 
Did you try for other columns, like, 2, 3, 4, ...: md.getTableName (2);

Maybe the first value is a generated one.
Code:
SELECT '4', now (), foo.x * bar.y FROM foo, bar WHERE bar.foo = foo.id;
4 and now () aren't bound to a table. x*y is from two.
Maybe something in this way?


don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top