Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CHECK IF A TABLE ALREADY EXISTS

 
Zaafir Barahim
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have been able to create a table in a database using java. now i want to check if a specific table exists in that database and if that table exists, then just insert values in it or else create a new table. PLEASE HELP ME OUT!!!
Thanks in advance for any help
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Connection.getMetaData().
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do with using the getTables method of DatabaseMetaData Interface. See the complete signature of this method on Java Doc
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do "SELECT 1 FROM [TABLE_NAME]" although then you'd have to do exception trapping which should be avoided if the table not existing is a common event.
 
Zaafir Barahim
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all. It works. Now i want to get all the tables in the database or just count the number of tables exists in my database. How do i do it. Iv got a code and when i execute it, i get an error saying that the feature is not present in my ACCESS DATABSE. Please help me out for new codes or for the prob. Thanks
 
Zaafir Barahim
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could i get also the data or the row count for each table found in the database
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The query SELECT TNAME, ROWNUM FROM TAB WHERE TABTYPE = 'TABLE' can be used to get the table name and the row number of that tablename.

Hope this may help you.
 
Zaafir Barahim
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it didnt work. Actually can i get some more information about what to replace in the select query. i have a database name "DATABASE", and i have 2 tables named "TABLE1" and "TABLE2". i am using MICROSOFT ACCESS DATABASE and JAVA to get all the tables in the database and the data inside each table. please help me out!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic