Java Utililty Methods SQL Table

List of utility methods to do SQL Table

Description

The list of methods to do SQL Table are organized into topic(s).

Method

StringnormalizeTableName(String table, Connection con)
normalize Table Name
for (String t : getTables(con)) {
    if (t.equalsIgnoreCase(table)) {
        return t;
return table;
Listparse(Connection connection, String tableName, String type)
parse
List<String> result = new ArrayList<>();
try {
    ResultSet resultSet = null;
    if (type.equals("primary")) {
        resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName);
    } else {
        resultSet = connection.getMetaData().getColumns(null, null, tableName, null);
    while (resultSet.next()) {
        result.add(resultSet.getString("COLUMN_NAME").toLowerCase());
} catch (SQLException e) {
    e.printStackTrace();
return result;
StringqueryAllFromTable(String tableName)
return a Statement with the result of a "SELECT *" query for a given table
return "SELECT * FROM " + tableName + ';';
StringquoteSchemaTable(Connection conn, String schema, String table)
Returns quoted schema & table to use in SQL queries for the given Connection.
String dbms = getDbmsFromConnection(conn);
return quoteSchemaTable(dbms, schema, table);
voidtruncateTable(Connection conn)
truncate Table
try (Statement statement = conn.createStatement()) {
    statement.execute("delete from TEST");
voidtruncateTable(Connection conn, String table_name)
truncate Table
Statement truncateStmt = null;
Statement countStmt = null;
String truncateSQL = "TRUNCATE TABLE " + table_name;
String countSQL = "SELECT COUNT(*) as count FROM " + table_name;
try {
    truncateStmt = conn.createStatement();
    truncateStmt.executeUpdate(truncateSQL);
    countStmt = conn.createStatement();
...
voidtruncateTable(Connection conn, String tablename)
truncate Table
StringBuilder sb = new StringBuilder();
sb.append("TRUNCATE TABLE ");
sb.append(tablename);
String sql = sb.toString();
Statement stmt = null;
try {
    stmt = conn.createStatement();
    stmt.executeUpdate(sql);
...