package com.nigorojr.typebest;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;

public abstract class Database {
    private String tableName;

    private final String protocol = "jdbc:derby:";
    private final String databaseFileName = "typebestDatabase.db";
    private final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    // Create database file if it doesn't exist
    private boolean create = true;

    private Connection connection = null;
    protected Statement statement = null;

    /**
     * Connects to the database file. Creates a new database file if none exists
     * and `create` is true.
     * 
     * @param tableName
     *            The name of the table that will be operated using this
     *            instance. The tableName will be upper-cased.
     * @throws SQLException
     *             When the constructor failed to create a new database file.
     */
    public Database(String tableName) throws SQLException {
        this.tableName = tableName.toUpperCase();

        try {
            Class.forName(driver).newInstance();
        }
        catch (InstantiationException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        connection = DriverManager.getConnection(protocol + databaseFileName
                + ";create=" + create);
        statement = connection.createStatement();
    }

    /**
     * Used to check whether the table exists in the database file.
     * 
     * @return True if a table exists, false otherwise.
     */
    public boolean isTableExist() {
        boolean tableExist = false;

        try {
            DatabaseMetaData meta = connection.getMetaData();
            ResultSet result = meta.getTables(null, null, tableName, null);
            tableExist = result.next();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return tableExist;
    }

    /**
     * Creates a table with the name given to the constructor.
     * 
     * @param pair
     *            A HashMap with name of the column as the key and the data type
     *            as the value.
     * @return True if the table was successfully created, false if something
     *         went wrong.
     */
    public boolean createTable(HashMap<String, String> pair) {
        String query = "";
        String columns = "";

        Iterator<String> iterator = pair.keySet().iterator();
        while (iterator.hasNext()) {
            String columnName = iterator.next();
            String dataType = pair.get(columnName);
            columns += columnName + " " + dataType;

            // Add a comma if it's not the last column
            if (iterator.hasNext())
                columns += ",";
        }
        // Don't accept empty HashMap
        if (columns.equals(""))
            return false;

        query = String.format("CREATE TABLE %s (%s)", tableName, columns);

        try {
            statement.execute(query);
        }
        catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /**
     * Getter method for tableName.
     * 
     * @return The table name of this instance.
     */
    public String getTableName() {
        return tableName;
    }

    abstract void insert(HashMap<String, String> pair);

    abstract ResultSet select(String condition);

    abstract void update(HashMap<String, String> pair);

    abstract void delete(String condition);

}

@@ -3816,10 +3816,1282 @@
 tion);%0A%0A
+    /**%0A     * This method is used to obtain a formatted String that can be used when%0A     * using the INSERT command. For example, given the HashMap with the%0A     * following entries:%0A     * %0A     * username =%3E 'test user'%0A     * id =%3E 2%0A     * layout =%3E 'QWERTY'%0A     * %0A     * the method will return a String:%0A     * (username, id, layout) VALUES ('test user', 2, 'QWERTY')%0A     * %0A     * @param pair%0A     *            The column names and the values that will be used to generate%0A     *            the String.%0A     * @return The formatted String in the form:%0A     *         %60(col1, col2, col3...) VALUES (val1, val2, val3...)%60%0A     */%0A    public String formatInsertQuery(HashMap%3CString, String%3E pair) %7B%0A        String ret = %22%22;%0A        String columnNames = %22%22;%0A        String values = %22%22;%0A%0A        Iterator%3CString%3E keys = pair.keySet().iterator();%0A        while (keys.hasNext()) %7B%0A            String key = keys.next();%0A            columnNames += key;%0A            values += pair.get(key);%0A            // Append comma if it's not the last element%0A            if (keys.hasNext()) %7B%0A                columnNames += %22, %22;%0A                values += %22, %22;%0A            %7D%0A        %7D%0A        ret = String.format(%22(%25s) VALUES (%25s)%22, columnNames, values);%0A        return ret;%0A    %7D%0A
 %7D%0A

Add method that formats a String specefically for INSERT SQL command