Here you can find the source of checkTableExists(DataSource dataSource, String tableName)
public static void checkTableExists(DataSource dataSource, String tableName) throws Exception
//package com.java2s; //License from project: Open Source License import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; public class Main { public static void checkTableExists(DataSource dataSource, String tableName) throws Exception { Connection connection = null; try {/*from w w w. j a v a 2s .c o m*/ connection = dataSource.getConnection(); checkTableExists(connection, tableName); } finally { closeQuietly(connection); } } public static void checkTableExists(Connection connection, String tableName) throws Exception { Statement statement = null; ResultSet resultSet = null; try { statement = connection.createStatement(); // Check if table exists try { resultSet = statement.executeQuery("SELECT * FROM " + tableName + " WHERE 1 = 0"); } catch (Exception e) { throw new Exception("Table '" + tableName + "' does not exist"); } } finally { closeQuietly(resultSet); resultSet = null; closeQuietly(statement); statement = null; } } public static void closeQuietly(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } public static void closeQuietly(Statement statement) { if (statement != null) { try { statement.close(); } catch (SQLException e) { } } } public static void closeQuietly(ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } } } }