BAEL-4789 Check if a database table exists (#10565)
* BAEL-4789 Check if a database table exists * BAEL-4789 fix db credentials * BAEL-4789 fix test method name
This commit is contained in:
parent
45764829f7
commit
dd288575da
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.tableexists;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
class DatabaseConfig {
|
||||||
|
static Connection connect() throws ClassNotFoundException, SQLException {
|
||||||
|
Class.forName("org.h2.Driver");
|
||||||
|
String url = "jdbc:h2:mem:testdb";
|
||||||
|
return DriverManager.getConnection(url, "user", "password");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void createTables(Connection connection) throws SQLException {
|
||||||
|
connection.createStatement().executeUpdate("create table EMPLOYEE (id int primary key auto_increment, name VARCHAR(255))");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dropTables(Connection connection) throws SQLException {
|
||||||
|
connection.createStatement().executeUpdate("drop table EMPLOYEE");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.tableexists;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
class TableChecker {
|
||||||
|
static void printAllTables(Connection connection, String tableName) throws SQLException {
|
||||||
|
DatabaseMetaData databaseMetaData = connection.getMetaData();
|
||||||
|
ResultSet resultSet = databaseMetaData.getTables(null, null, tableName, new String[] {"TABLE"});
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
String name = resultSet.getString("TABLE_NAME");
|
||||||
|
String schema = resultSet.getString("TABLE_SCHEM");
|
||||||
|
System.out.println(name + " on schema " + schema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean tableExists(Connection connection, String tableName) throws SQLException {
|
||||||
|
DatabaseMetaData meta = connection.getMetaData();
|
||||||
|
ResultSet resultSet = meta.getTables(null, null, tableName, new String[] {"TABLE"});
|
||||||
|
|
||||||
|
return resultSet.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean tableExistsSQL(Connection connection, String tableName) throws SQLException {
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement("SELECT count(*) " +
|
||||||
|
"FROM information_schema.tables " +
|
||||||
|
"WHERE table_name = ?" +
|
||||||
|
"LIMIT 1;");
|
||||||
|
preparedStatement.setString(1, tableName);
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
resultSet.next();
|
||||||
|
return resultSet.getInt(1) != 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.baeldung.tableexists;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class TableCheckerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCreatedTable_shouldFindTable() throws SQLException, ClassNotFoundException {
|
||||||
|
// given
|
||||||
|
Connection connection = DatabaseConfig.connect();
|
||||||
|
DatabaseConfig.createTables(connection);
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean tableExists = TableChecker.tableExists(connection, "EMPLOYEE");
|
||||||
|
|
||||||
|
// then
|
||||||
|
TableChecker.printAllTables(connection, null);
|
||||||
|
assertTrue(tableExists);
|
||||||
|
|
||||||
|
DatabaseConfig.dropTables(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCreatedTable_shouldFindTableWithSQL() throws SQLException, ClassNotFoundException {
|
||||||
|
// given
|
||||||
|
Connection connection = DatabaseConfig.connect();
|
||||||
|
DatabaseConfig.createTables(connection);
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean tableExists = TableChecker.tableExistsSQL(connection, "EMPLOYEE");
|
||||||
|
|
||||||
|
// then
|
||||||
|
TableChecker.printAllTables(connection, null);
|
||||||
|
assertTrue(tableExists);
|
||||||
|
|
||||||
|
DatabaseConfig.dropTables(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNoTable_shouldNotFindTable() throws SQLException, ClassNotFoundException {
|
||||||
|
// given
|
||||||
|
Connection connection = DatabaseConfig.connect();
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean tableExists = TableChecker.tableExists(connection, "EMPLOYEE");
|
||||||
|
|
||||||
|
// then
|
||||||
|
TableChecker.printAllTables(connection, null);
|
||||||
|
assertFalse(tableExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNoTable_shouldNotFindTableWithSQL() throws SQLException, ClassNotFoundException {
|
||||||
|
// given
|
||||||
|
Connection connection = DatabaseConfig.connect();
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean tableExists = TableChecker.tableExistsSQL(connection, "EMPLOYEE");
|
||||||
|
|
||||||
|
// then
|
||||||
|
TableChecker.printAllTables(connection, null);
|
||||||
|
assertFalse(tableExists);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user