Here you can find the source of hasTable(DataSource ds, String table)
Parameter | Description |
---|---|
ds | DataSource for creating connections to database in question. |
table | table name, case insensitive. |
Parameter | Description |
---|---|
SQLException | if there is a problem executing the check. |
true
if the database contains the table.
public static boolean hasTable(DataSource ds, String table) throws SQLException
//package com.java2s; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; public class Main { /**/* ww w. jav a 2s .c o m*/ * Checks if the given database contains a table with the given name. * * @param ds DataSource for creating connections to database in question. * @param table table name, case insensitive. * @return <code>true</code> if the database contains the table. * @throws SQLException if there is a problem executing the check. */ public static boolean hasTable(DataSource ds, String table) throws SQLException { try (Connection conn = ds.getConnection()) { return hasTable(conn, table); } } /** * Checks if the given database contains a table with the given name. * * @param conn database connection. * @param table table name, case insensitive. * @return <code>true</code> if the database contains the table. * @throws SQLException if there is a problem executing the check. */ public static boolean hasTable(Connection conn, String table) throws SQLException { DatabaseMetaData md = conn.getMetaData(); try (ResultSet tables = md.getTables(null, null, null, null)) { boolean result = false; while (tables.next()) { if (tables.getString("TABLE_NAME").equalsIgnoreCase(table)) { result = true; } } return result; } } }