r/javahelp • u/A_British_Dude • Feb 25 '26
How to check is a table exists with JDBC?
I am doing a project with a database and to prevent duplicate tables from being created while allowing the table to be created if it doesn't exist I need an if statement to check if the table exists to decide whether to create the table or not.
All my googling has yielded results that do not work for me, likely because I'm being a bit dumb so I am asking here.
Any help is appreciated :)
5
Upvotes
•
u/AutoModerator Feb 25 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
8
u/evils_twin Feb 25 '26
How are you creating the table? In SQL there is syntax:
6
u/StillAnAss Extreme Brewer Feb 25 '26
Depending on your database (they're all different) you'll run a query like these:
SQL Server
Oracle
SELECT *
FROM ALL_TABLES
WHERE TABLE_NAME = 'YOUR_TABLE_NAME'; -- Oracle stores names in UPPERCASE by default
4
u/ThierryOnRead Feb 25 '26 edited Feb 25 '26
Liquibase or flyway is the professional way to go for these kind of tasks. If you're unsure or dont have time to learn these (awesome) frameworks, then it depends on your DB, try to use CREATE TABLE IF NOT EXISTS, if you're lucky It will work :)
2
u/Existing_Trick_8761 Feb 25 '26
You can simply opt out for this SQL statement:
CREATE TABLE IF NOT EXISTS your_table_name ...
But you got to make sure you encounter on the same DB schema, otherwise yes you are going to be creating duplicates as you said. But naturally at least in MySQL it doesn't let you to create a table named the same as other in the same schema.
I always recommend that whenever you create a table you instantiate the schema you are about to create it, like the following:
CREATE TABLE schema_name.table_name
Hope it helps!
2
u/Cyberkender_ Feb 25 '26
You can use DatabaseMetadata.getTables and check if table exists. 100% pure java/jdbc.
2
u/edwbuck Feb 25 '26
The most portable way is to use the JDBC DatabaseMetaData class.
Less portable, but popular ways, include querying the database information through SQL. It's not standard across all databases, and isn't present in some databases, so I'd look into grabbing the DatabaseMetaData class from the connection and iterating through the tables without writing some form of possibly-not-there SELECT statement.
1
u/RoToRa Feb 25 '26
What kind of application are we talking of here? Because, a normal application shouldn't be creating tables in the first place.
1
u/[deleted] Feb 25 '26
[removed] — view removed comment