You're requesting all tables from the server and then browsing through those names locally. That is not very efficient if you just want to check if table 'X' exist. You would want to use the 3rd argument to the getTables() method !!! (rather than using 'null' as you do)
– peterhJul 7 '13 at 18:36

this solution doesn't work if the table name is provided with schema, so be careful with it. To provide the schema, you should use meta.getTables("Schema_Name", null, "My_Table_Name", new String[] {"TABLE"})
– ikryvorotenkoDec 23 '16 at 16:12

@BrianAgnew Can I run this command even if I don't have retrieval permissions ?
– Anup Kumar GuptaAug 25 '17 at 9:21

1

@AnupKumarGupta: That depends on the DBMS you use, but probably yes. Usually just listing the tables available is not restriced.
– sleskeOct 30 '18 at 9:12

Ensure that when you use table names that it is not using pattern chars like underscore, such as "employee_reports". I have been caught by situations like this as some metadata implementations use pattern matching. Best to double check the result set with a simple table.equals("employee_reports") statement in your if
– ConstantinOct 18 '14 at 17:05

I don't actually find any of the presented solutions here to be fully complete so I'll add my own. Nothing new here. You can stitch this together from the other presented solutions plus various comments.

There are at least two things you'll have to make sure:

Make sure you pass the table name to the getTables() method,
rather than passing a null value. In the first case you let the
database server filter the result for you, in the second you request
a list of all tables from the server and then filter the list
locally. The former is much faster if you are only searching for a
single table.

Make sure to check the table name from the resultset with an equals
match. The reason is that the getTables() does pattern matching on
the query for the table and the _ character is a wildcard in SQL.
Suppose you are checking for the existence of a table named
EMPLOYEE_SALARY. You'll then get a match on EMPLOYEESSALARY too
which is not what you want.

You may want to consider what you pass as the types parameter (4th parameter) on your getTables() call. Normally I would just leave at null because you don't want to restrict yourself. A VIEW is as good as a TABLE, right? These days many databases allow you to update through a VIEW so restricting yourself to only TABLE type is in most cases not the way to go. YMMV.

You may need to change tName.equals(tableName) to tName.equals(tableName.toLowerCase()), it wouldn't work if tableName were uppercase.
– SeareneNov 30 '16 at 9:09

@Searene.The question isn't about any particular database. You are right that case needs to considered, but this depends on the database. Some databases you can even change, for example MS SQL server support both case-insensitive and case-sensitive depending on how you set the value of the 'COLLATION'. So your point is valid, it is just that is is difficult to come up with a general solution that will work with any scenario.
– peterhMar 4 at 17:06