Skip to content

Instantly share code, notes, and snippets.

@SnapperGee
Last active April 12, 2023 06:49
Show Gist options
  • Save SnapperGee/df3382bce2d6f1741d7902393ec73148 to your computer and use it in GitHub Desktop.
Save SnapperGee/df3382bce2d6f1741d7902393ec73148 to your computer and use it in GitHub Desktop.
Java class for getting general info (table and column names) from SQLite Databases
package countit.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public final class Table
{
private final Set<String> _names;
private final Map<String, Set<String>> _columns;
private final Map<String, Integer> _numberOfRecords;
private final String _string;
private final int _hashCode;
public Table(Connection dbConnection) throws SQLException
{
final ResultSet tablesResultSet =
dbConnection.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
final Map<String, Set<String>> columnNames = new HashMap<>();
final Map<String, Integer> numberOfRecords = new HashMap<>();
while (tablesResultSet.next())
{
final String tableName = tablesResultSet.getString("Table_NAME");
final ResultSet columnsResultSet =
dbConnection.getMetaData().getColumns(null, null, tableName, null);
final HashSet<String> columnNamesSet = new HashSet<>();
while (columnsResultSet.next())
{
columnNamesSet.add(columnsResultSet.getString("COLUMN_NAME"));
}
columnNames.put(tableName, columnNamesSet);
numberOfRecords.put(tableName, columnNamesSet.size());
}
this._columns = Collections.unmodifiableMap(columnNames);
this._names = Collections.unmodifiableSet(columnNames.keySet());
this._numberOfRecords = Collections.unmodifiableMap(numberOfRecords);
this._hashCode = this._columns.hashCode();
this._string = Table.class.getSimpleName() + '(' + this._names.size() + ") " + this._columns.toString();
}
public Set<String> getTableNames() { return this._names; }
public Map<String, Set<String>> getColumnNames() { return this._columns; }
public Map<String, Integer> getNumberOfRecords() { return this._numberOfRecords; }
@Override
public boolean equals(Object obj)
{
if (this == obj) { return true; }
if (obj == null) {return false; }
if ( ! (obj instanceof Table)) { return false; }
return this._columns.equals(((Table) obj)._columns);
}
@Override
public int hashCode() { return this._hashCode; }
@Override
public String toString() { return this._string; }
public static Table connectionFromUrl(String url) throws SQLException
{
return new Table(DriverManager.getConnection(url));
}
public static Table connectionFromUrl(String url, String user, String password) throws SQLException
{
return new Table(DriverManager.getConnection(url, user, password));
}
public static Table connectionFromUrl(String url, Properties info) throws SQLException
{
return new Table(DriverManager.getConnection(url, info));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment