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

voidclearTables(Connection conn)
Clear the tables.
Statement stmt = conn.createStatement();
try {
    stmt.execute("delete from id_sequences where name = 'phase'");
    stmt.execute("delete from phase_dependency");
    stmt.execute("delete from phase_criteria");
    stmt.execute("delete from phase_criteria_type_lu");
    stmt.execute("delete from project_phase");
    stmt.execute("delete from phase_type_lu");
...
voidclearTables(Connection conn, String[] tables)

Clear the given tables from the database.

Statement stmt = conn.createStatement();
try {
    for (int i = 0; i < tables.length; i++) {
        stmt.executeUpdate("DELETE FROM " + tables[i]);
} finally {
    stmt.close();
voidclearTables(Connection connection)
Deleting all data from specified table.
String[] tables = new String[] { "scorecard_question", "scorecard_section", "scorecard_group", "scorecard",
        "scorecard_status_lu", "scorecard_type_lu", "scorecard_question_type_lu", "project_category_lu" };
for (int i = 0; i < tables.length; i++) {
    String query = "DELETE FROM " + tables[i];
    Statement statement = connection.createStatement();
    statement.executeUpdate(query);
booleancolumnExist(String table, String column, Connection con)
column Exist
return getColumns(table, con).contains(column.toUpperCase());
ObjectconstructObject(Class theClass, Connection db, int objectId, String tableName, String uniqueField)
Description of the Method
try {
    Class[] paramClass = new Class[] { Class.forName("java.sql.Connection"), int.class,
            Class.forName("java.lang.String"), Class.forName("java.lang.String") };
    Constructor constructor = theClass.getConstructor(paramClass);
    Object[] paramObject = new Object[] { db, new Integer(objectId), tableName, uniqueField };
    return constructor.newInstance(paramObject);
} catch (Exception e) {
    return null;
...
longcount(Connection conn, String table)
count
long ret = 0;
try {
    Statement statement = conn.createStatement();
    String sql = String.format("select count(*) from %s;", table);
    ResultSet set = statement.executeQuery(sql);
    if (set.next()) {
        ret = set.getLong(1);
    statement.close();
} catch (SQLException e) {
    e.printStackTrace();
return ret;
intcountTables(Connection connection)
count Tables
int count = 0;
ResultSet resultSet = connection.getMetaData().getTables("", null, null, new String[] { "TABLE" });
while (resultSet.next()) {
    count++;
resultSet.close();
return count;
voidcreateBagValuesTables(Connection con)
Create the table 'bagvalues' containing the values of the key field objects contained in a bag and an extra values
String sqlTable = "CREATE TABLE bagvalues (savedbagid integer, value text, extra text)";
String sqlIndex = "CREATE UNIQUE INDEX bagvalues_index1 ON bagvalues " + "(savedbagid, value, extra)";
con.createStatement().execute(sqlTable);
con.createStatement().execute(sqlIndex);
voidcreateFreqTable()
create Freq Table
try {
    Statement stmt = connection.createStatement();
    String sql = "CREATE TABLE IF NOT EXISTS " + FREQ_TABLE + "(" + ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + CHROME + " CHAR(10) NOT NULL," + POSITION
            + " CHAR(20) NOT NULL," + A + " CHAR(10)," + T + " CHAR(10)," + C + " CHAR(10)," + G
            + " CHAR(10), " + "UNIQUE (" + CHROME + "," + POSITION + ")" + ");";
    stmt.executeUpdate(sql);
    stmt.close();
...
voidcreateHSQLTables(LineNumberReader reader, Statement statement)
create HSQL Tables
String line;
int idx = 0;
String addPkLine = null;
String createTableLine = null;
StringBuffer stringBuffer = new StringBuffer(1000);
while (null != (line = reader.readLine())) {
    if (line.startsWith("alter table ")) {
    } else {
...