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

ListgetAllTableNames(Connection connection)
Returns a list of all table names in the database schema accessible by the given connection.
List<String> tables = new ArrayList<String>();
ResultSet rs = connection.getMetaData().getTables(null, null, null, null);
while (rs.next())
    tables.add(rs.getString(3));
rs.close();
return tables;
ListgetAllTables(Connection conn)
get All Tables
List<String> tableNameList = new ArrayList<String>();
Statement stmt = null;
ResultSet rs = null;
try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery(SQL_SHOW_TABLES);
    while (rs.next()) {
        tableNameList.add(rs.getString(1));
...
StringgetBatchResultMessage(String tableName, int rowIdx, int resultCode)
Returns the logging message corresponding to the given result code of a batch message.
if (resultCode < 0) {
    try {
        if (resultCode == Statement.class.getField("SUCCESS_NO_INFO").getInt(null)) {
            return null;
        } else if (resultCode == Statement.class.getField("EXECUTE_FAILED").getInt(null)) {
            return "The batch insertion of row " + rowIdx + " into table " + tableName
                    + " failed but the driver is able to continue processing";
        } else {
...
intgetCount(Connection conn, String tableName)
get Count
String sql = "select count(*) from " + tableName;
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(sql);
rset.next();
int count = rset.getInt(1);
rset.close();
stmt.close();
return count;
...
intgetIdNumber(String tableName)
get Id Number
try {
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
    int count = 0;
    while (rs.next()) {
        count++;
    return count + 1;
...
Map,Object>getImmutableDefaults()
get Immutable Defaults
if (immutableDefaults == null || immutableDefaults.isEmpty()) {
    makeImmutableDefaultsMap();
return immutableDefaults;
intgetLastCreatedEntry(Connection conn, String tableName)
get Last Created Entry
int id = -1;
String sql = "SELECT id FROM " + tableName + " ORDER BY id DESC LIMIT 1";
Statement st = conn.createStatement();
try {
    ResultSet resultSet = st.executeQuery(sql);
    if (resultSet.next()) {
        id = resultSet.getInt("id");
    } else
...
MapgetMetaDataMap(Statement statement, String tableOrViewName)
get Meta Data Map
Map<String, String[]> metaDataMap = new Hashtable<String, String[]>();
String tmpColumnClassName = null;
String tmpColumnName = null;
int tmpColumnTypeId = 0;
String metaDataQuery = "SELECT * FROM " + tableOrViewName + " WHERE 1 = 2";
ResultSet resultSet = statement.executeQuery(metaDataQuery);
ResultSetMetaData metaData = resultSet.getMetaData();
for (int i = 1; i < metaData.getColumnCount() + 1; i++) {
...
intgetNextId(Connection con, String table, String identityFieldName)
get Next Id
Statement stmt = con.createStatement();
try {
    ResultSet rs = stmt.executeQuery(
            "select max(" + identityFieldName + ") as " + identityFieldName + " from " + table);
    try {
        if (rs.next()) {
            return rs.getInt(1) + 1;
        } else {
...
intgetRowCount(Statement stmt, String tableName)
Returns the row count of the given table
int rowCount = 0;
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
if (rs.next()) {
    rowCount = rs.getInt(1);
rs.close();
return rowCount;