Search in sources :

Example 16 with Connection

use of java.sql.Connection in project groovy by apache.

the class Sql method execute.

/**
     * Executes the given piece of SQL with parameters.
     * Also calls the provided processResults Closure to process any ResultSet or UpdateCount results that executing the SQL might produce.
     * <p>
     * This method supports named and named ordinal parameters.
     * See the class Javadoc for more details.
     * <p>
     * Resource handling is performed automatically where appropriate.
     *
     * @param sql    the SQL statement
     * @param params a list of parameters
     * @param processResults a Closure which will be passed two parameters: either {@code true} plus a list of GroovyRowResult values
     *                       derived from {@code statement.getResultSet()} or {@code false} plus the update count from {@code statement.getUpdateCount()}.
     *                       The closure will be called for each result produced from executing the SQL.
     * @throws SQLException if a database access error occurs
     * @see #execute(String, Closure)
     * @since 2.3.2
     */
public void execute(String sql, List<Object> params, Closure processResults) throws SQLException {
    Connection connection = createConnection();
    PreparedStatement statement = null;
    try {
        statement = getPreparedStatement(connection, sql, params);
        boolean isResultSet = statement.execute();
        int updateCount = statement.getUpdateCount();
        while (isResultSet || updateCount != -1) {
            if (processResults.getMaximumNumberOfParameters() != 2) {
                throw new SQLException("Incorrect number of parameters for processResults Closure");
            }
            if (isResultSet) {
                ResultSet resultSet = statement.getResultSet();
                List<GroovyRowResult> rowResult = resultSet == null ? null : asList(sql, resultSet);
                processResults.call(isResultSet, rowResult);
            } else {
                processResults.call(isResultSet, updateCount);
            }
            isResultSet = statement.getMoreResults();
            updateCount = statement.getUpdateCount();
        }
    } catch (SQLException e) {
        LOG.warning("Failed to execute: " + sql + " because: " + e.getMessage());
        throw e;
    } finally {
        closeResources(connection, statement);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with Connection

use of java.sql.Connection in project hadoop by apache.

the class TestDbClasses method testOracleDBRecordReader.

/**
   * test generate sql script for OracleDBRecordReader.
   */
@Test(timeout = 20000)
public void testOracleDBRecordReader() throws Exception {
    DBInputSplit splitter = new DBInputSplit(1, 10);
    Configuration configuration = new Configuration();
    Connection connect = DriverForTest.getConnection();
    DBConfiguration dbConfiguration = new DBConfiguration(configuration);
    dbConfiguration.setInputOrderBy("Order");
    String[] fields = { "f1", "f2" };
    OracleDBRecordReader<NullDBWritable> recorder = new OracleDBRecordReader<NullDBWritable>(splitter, NullDBWritable.class, configuration, connect, dbConfiguration, "condition", fields, "table");
    assertEquals("SELECT * FROM (SELECT a.*,ROWNUM dbif_rno FROM ( SELECT f1, f2 FROM table WHERE condition ORDER BY Order ) a WHERE rownum <= 10 ) WHERE dbif_rno > 1", recorder.getSelectQuery());
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) NullDBWritable(org.apache.hadoop.mapreduce.lib.db.DBInputFormat.NullDBWritable) Connection(java.sql.Connection) DBInputSplit(org.apache.hadoop.mapreduce.lib.db.DBInputFormat.DBInputSplit) DataDrivenDBInputSplit(org.apache.hadoop.mapreduce.lib.db.DataDrivenDBInputFormat.DataDrivenDBInputSplit) Test(org.junit.Test)

Example 18 with Connection

use of java.sql.Connection in project hive by apache.

the class Commands method getConfInternal.

/**
   * Use call statement to retrieve the configurations for substitution and sql for the substitution.
   *
   * @param call
   * @return
   */
private BufferedRows getConfInternal(boolean call) {
    Statement stmnt = null;
    BufferedRows rows = null;
    try {
        boolean hasResults = false;
        DatabaseConnection dbconn = beeLine.getDatabaseConnection();
        Connection conn = null;
        if (dbconn != null)
            conn = dbconn.getConnection();
        if (conn != null) {
            if (call) {
                stmnt = conn.prepareCall("set");
                hasResults = ((CallableStatement) stmnt).execute();
            } else {
                stmnt = beeLine.createStatement();
                hasResults = stmnt.execute("set");
            }
        }
        if (hasResults) {
            ResultSet rs = stmnt.getResultSet();
            rows = new BufferedRows(beeLine, rs);
        }
    } catch (SQLException e) {
        beeLine.error(e);
    } finally {
        if (stmnt != null) {
            try {
                stmnt.close();
            } catch (SQLException e1) {
                beeLine.error(e1);
            }
        }
    }
    return rows;
}
Also used : SQLException(java.sql.SQLException) HiveStatement(org.apache.hive.jdbc.HiveStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 19 with Connection

use of java.sql.Connection in project hive by apache.

the class HiveSchemaTool method showInfo.

/***
   * Print Hive version and schema version
   * @throws MetaException
   */
public void showInfo() throws HiveMetaException {
    Connection metastoreConn = getConnectionToMetastore(true);
    String hiveVersion = MetaStoreSchemaInfo.getHiveSchemaVersion();
    String dbVersion = getMetaStoreSchemaVersion(metastoreConn);
    System.out.println("Hive distribution version:\t " + hiveVersion);
    System.out.println("Metastore schema version:\t " + dbVersion);
    assertCompatibleVersion(hiveVersion, dbVersion);
}
Also used : Connection(java.sql.Connection)

Example 20 with Connection

use of java.sql.Connection in project hive by apache.

the class HiveSchemaTool method doValidate.

public void doValidate() throws HiveMetaException {
    System.out.println("Starting metastore validation\n");
    Connection conn = getConnectionToMetastore(false);
    boolean success = true;
    try {
        if (validateSchemaVersions(conn)) {
            System.out.println("[SUCCESS]\n");
        } else {
            success = false;
            System.out.println("[FAIL]\n");
        }
        if (validateSequences(conn)) {
            System.out.println("[SUCCESS]\n");
        } else {
            success = false;
            System.out.println("[FAIL]\n");
        }
        if (validateSchemaTables(conn)) {
            System.out.println("[SUCCESS]\n");
        } else {
            success = false;
            System.out.println("[FAIL]\n");
        }
        if (validateLocations(conn, this.validationServers)) {
            System.out.println("[SUCCESS]\n");
        } else {
            success = false;
            System.out.println("[FAIL]\n");
        }
        if (validateColumnNullValues(conn)) {
            System.out.println("[SUCCESS]\n");
        } else {
            success = false;
            System.out.println("[FAIL]\n");
        }
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new HiveMetaException("Failed to close metastore connection", e);
            }
        }
    }
    System.out.print("Done with metastore validation: ");
    if (!success) {
        System.out.println("[FAIL]");
        System.exit(1);
    } else {
        System.out.println("[SUCCESS]");
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException)

Aggregations

Connection (java.sql.Connection)6326 PreparedStatement (java.sql.PreparedStatement)2793 ResultSet (java.sql.ResultSet)2657 Test (org.junit.Test)2455 SQLException (java.sql.SQLException)2267 Properties (java.util.Properties)1188 Statement (java.sql.Statement)1078 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)397 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 DataSource (javax.sql.DataSource)211 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)192 IOException (java.io.IOException)158 Reader (java.io.Reader)144 DatabaseMetaData (java.sql.DatabaseMetaData)144 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 HashMap (java.util.HashMap)123 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)114 Timestamp (java.sql.Timestamp)113