Search in sources :

Example 51 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project jdbc-shards by wplatform.

the class BatchUpdatesTestCase method testCoffee.

private void testCoffee() throws SQLException {
    conn = getConnection();
    stat = conn.createStatement();
    DatabaseMetaData meta = conn.getMetaData();
    assertTrue(meta.supportsBatchUpdates());
    stat.executeUpdate("CREATE TABLE TEST(KEY_ID INT PRIMARY KEY," + "C_NAME VARCHAR(255),PRICE DECIMAL(20,2),TYPE_ID INT)");
    String newName = null;
    float newPrice = 0;
    int newType = 0;
    prep = conn.prepareStatement("INSERT INTO TEST VALUES(?,?,?,?)");
    int newKey = 1;
    for (int i = 1; i <= COFFEE_TYPE && newKey <= COFFEE_SIZE; i++) {
        for (int j = 1; j <= i && newKey <= COFFEE_SIZE; j++) {
            newName = "COFFEE-" + newKey;
            newPrice = newKey + (float) .00;
            newType = i;
            prep.setInt(1, newKey);
            prep.setString(2, newName);
            prep.setFloat(3, newPrice);
            prep.setInt(4, newType);
            prep.execute();
            newKey = newKey + 1;
        }
    }
    trace("Inserted the Rows ");
    testAddBatch01();
    testAddBatch02();
    testClearBatch01();
    testClearBatch02();
    testExecuteBatch01();
    testExecuteBatch02();
    testExecuteBatch03();
    testExecuteBatch04();
    testExecuteBatch05();
    testExecuteBatch06();
    testExecuteBatch07();
    testContinueBatch01();
    conn.close();
}
Also used : DatabaseMetaData(java.sql.DatabaseMetaData)

Example 52 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project symmetric-ds by JumpMind.

the class AbstractJdbcDdlReader method getTableNames.

public List<String> getTableNames(final String catalog, final String schema, final String[] tableTypes) {
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    List<String> list = sqlTemplate.execute(new IConnectionCallback<List<String>>() {

        public List<String> execute(Connection connection) throws SQLException {
            ArrayList<String> list = new ArrayList<String>();
            DatabaseMetaData meta = connection.getMetaData();
            ResultSet rs = null;
            try {
                rs = meta.getTables(catalog, schema, null, tableTypes);
                while (rs.next()) {
                    String tableName = rs.getString("TABLE_NAME");
                    if (tableName == null) {
                        tableName = rs.getString("NAME");
                    }
                    list.add(tableName);
                }
                return list;
            } finally {
                close(rs);
            }
        }
    });
    return list;
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 53 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project symmetric-ds by JumpMind.

the class JdbcDatabasePlatformFactory method isRedshiftDatabase.

private static boolean isRedshiftDatabase(Connection connection) {
    boolean isRedshift = false;
    try {
        DatabaseMetaData dmd = connection.getMetaData();
        dmd.getMaxColumnsInIndex();
        if (dmd.getDriverName().toUpperCase().contains("REDSHIFT")) {
            isRedshift = true;
        }
    } catch (SQLException ex) {
        if (ex.getSQLState().equals("99999")) {
            isRedshift = true;
        }
    }
    return isRedshift;
}
Also used : SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 54 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project AuthMeReloaded by AuthMe.

the class MySQL method checkTablesAndColumns.

/**
     * Creates the table or any of its required columns if they don't exist.
     */
private void checkTablesAndColumns() throws SQLException {
    try (Connection con = getConnection();
        Statement st = con.createStatement()) {
        // Create table with ID column if it doesn't exist
        String sql = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + col.ID + " MEDIUMINT(8) UNSIGNED AUTO_INCREMENT," + "PRIMARY KEY (" + col.ID + ")" + ") CHARACTER SET = utf8;";
        st.executeUpdate(sql);
        DatabaseMetaData md = con.getMetaData();
        if (isColumnMissing(md, col.NAME)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE AFTER " + col.ID + ";");
        }
        if (isColumnMissing(md, col.REAL_NAME)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL AFTER " + col.NAME + ";");
        }
        if (isColumnMissing(md, col.PASSWORD)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;");
        }
        if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);");
        }
        if (isColumnMissing(md, col.IP)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.IP + " VARCHAR(40) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;");
        }
        if (isColumnMissing(md, col.LAST_LOGIN)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;");
        } else {
            migrateLastLoginColumn(con, md);
        }
        if (isColumnMissing(md, col.LASTLOC_X)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LAST_LOGIN + " , ADD " + col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_X + " , ADD " + col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_Y);
        } else {
            st.executeUpdate("ALTER TABLE " + tableName + " MODIFY " + col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0', MODIFY " + col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0', MODIFY " + col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0';");
        }
        if (isColumnMissing(md, col.LASTLOC_WORLD)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + col.LASTLOC_Z);
        }
        if (isColumnMissing(md, col.LASTLOC_YAW)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_YAW + " FLOAT;");
        }
        if (isColumnMissing(md, col.LASTLOC_PITCH)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_PITCH + " FLOAT;");
        }
        if (isColumnMissing(md, col.EMAIL)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com' AFTER " + col.LASTLOC_WORLD);
        }
        if (isColumnMissing(md, col.IS_LOGGED)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.EMAIL);
        }
    }
    ConsoleLogger.info("MySQL setup finished");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 55 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project AuthMeReloaded by AuthMe.

the class SQLite method setup.

@VisibleForTesting
protected void setup() throws SQLException {
    try (Statement st = con.createStatement()) {
        // Note: cannot add unique fields later on in SQLite, so we add it on initialization
        st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" + col.ID + " INTEGER AUTO_INCREMENT, " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE, " + "CONSTRAINT table_const_prim PRIMARY KEY (" + col.ID + "));");
        DatabaseMetaData md = con.getMetaData();
        if (isColumnMissing(md, col.REAL_NAME)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL DEFAULT 'Player';");
        }
        if (isColumnMissing(md, col.PASSWORD)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) NOT NULL DEFAULT '';");
        }
        if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);");
        }
        if (isColumnMissing(md, col.IP)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.IP + " VARCHAR(40) NOT NULL DEFAULT '';");
        }
        if (isColumnMissing(md, col.LAST_LOGIN)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LAST_LOGIN + " TIMESTAMP;");
        }
        if (isColumnMissing(md, col.LASTLOC_X)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0';");
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0';");
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0';");
        }
        if (isColumnMissing(md, col.LASTLOC_WORLD)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world';");
        }
        if (isColumnMissing(md, col.LASTLOC_YAW)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_YAW + " FLOAT;");
        }
        if (isColumnMissing(md, col.LASTLOC_PITCH)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_PITCH + " FLOAT;");
        }
        if (isColumnMissing(md, col.EMAIL)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com';");
        }
        if (isColumnMissing(md, col.IS_LOGGED)) {
            st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.IS_LOGGED + " INT DEFAULT '0';");
        }
    }
    ConsoleLogger.info("SQLite Setup finished");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) DatabaseMetaData(java.sql.DatabaseMetaData) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)360 ResultSet (java.sql.ResultSet)215 Connection (java.sql.Connection)169 SQLException (java.sql.SQLException)154 Test (org.junit.Test)87 Statement (java.sql.Statement)49 ResultSetMetaData (java.sql.ResultSetMetaData)42 ArrayList (java.util.ArrayList)40 PreparedStatement (java.sql.PreparedStatement)38 IOException (java.io.IOException)26 Properties (java.util.Properties)25 HashSet (java.util.HashSet)16 PhoenixDatabaseMetaData (org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)16 HashMap (java.util.HashMap)15 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 PrintWriter (java.io.PrintWriter)10 Savepoint (java.sql.Savepoint)10 List (java.util.List)10 Map (java.util.Map)9 DataSource (javax.sql.DataSource)9