Example usage for java.sql CallableStatement wasNull

List of usage examples for java.sql CallableStatement wasNull

Introduction

In this page you can find the example usage for java.sql CallableStatement wasNull.

Prototype

boolean wasNull() throws SQLException;

Source Link

Document

Retrieves whether the last OUT parameter read had the value of SQL NULL.

Usage

From source file:org.apache.hadoop.metrics2.sink.SqlServerSink.java

public long getMetricRecordID(String recordTypeContext, String recordTypeName, String nodeName, String sourceIP,
        String clusterName, String serviceName, String tagPairs, long recordTimestamp) {
    CallableStatement cstmt = null;
    long result;//ww  w  .  j  a v  a 2s.  c  o m
    if (recordTypeContext == null || recordTypeName == null || nodeName == null || sourceIP == null
            || tagPairs == null)
        return -1;

    int colid = 1;
    try {
        if (ensureConnection()) {
            cstmt = conn.prepareCall("{call dbo.uspGetMetricRecord(?, ?, ?, ?, ?, ?, ?, ?, ?)}");
            cstmt.setNString(colid++, recordTypeContext);
            cstmt.setNString(colid++, recordTypeName);
            cstmt.setNString(colid++, nodeName);
            cstmt.setNString(colid++, sourceIP);
            cstmt.setNString(colid++, clusterName);
            cstmt.setNString(colid++, serviceName);
            cstmt.setNString(colid++, tagPairs);
            cstmt.setLong(colid++, recordTimestamp);
            cstmt.registerOutParameter(colid, java.sql.Types.BIGINT);
            cstmt.execute();
            result = cstmt.getLong(colid);
            if (cstmt.wasNull())
                return -1;
            return result;
        }
    } catch (Exception e) {
        if (DEBUG)
            logger.info("Error during getMetricRecordID call sproc: " + e.toString());
        flush();
    } finally {
        if (cstmt != null) {
            try {
                cstmt.close();
            } catch (SQLException se) {
                if (DEBUG)
                    logger.info("Error during getMetricRecordID close cstmt: " + se.toString());
            }
            /*
             * We don't close the connection here because we are likely to be
             * writing
             * metric values next and it is more efficient to share the connection.
             */
        }
    }
    return -1;
}

From source file:org.apache.hadoop.metrics2.sink.SqlServerSinkHadoop1Test.java

@Override
@Test//from   w  w w. j av a  2 s. c o m
public void testPutMetrics() throws Exception {
    SubsetConfiguration configuration = createNiceMock(SubsetConfiguration.class);
    Connection connection = createNiceMock(Connection.class);
    CallableStatement cstmt = createNiceMock(CallableStatement.class);
    MetricsRecord record = createNiceMock(MetricsRecord.class);
    Metric metric = createNiceMock(Metric.class);

    // set expectations
    expect(configuration.getParent()).andReturn(null);
    expect(configuration.getPrefix()).andReturn("prefix");
    expect(configuration.getString("databaseUrl")).andReturn("url");

    expect(record.context()).andReturn("context");
    expect(record.name()).andReturn("typeName");
    expect(record.tags()).andReturn(new HashSet<MetricsTag>());
    expect(record.timestamp()).andReturn(9999L);

    expect(record.metrics()).andReturn(Collections.singleton(metric));

    expect(metric.name()).andReturn("name").anyTimes();
    expect(metric.value()).andReturn(1234);

    expect(connection.prepareCall("{call dbo.uspGetMetricRecord(?, ?, ?, ?, ?, ?, ?, ?, ?)}")).andReturn(cstmt);
    cstmt.setNString(1, "context");
    cstmt.setNString(2, "typeName");
    cstmt.setNString(eq(3), (String) anyObject());
    cstmt.setNString(eq(4), (String) anyObject());
    cstmt.setNString(eq(5), (String) anyObject());
    cstmt.setNString(6, "prefix");
    cstmt.setNString(7, "sourceName:prefix");
    cstmt.setLong(8, 9999L);
    cstmt.registerOutParameter(9, java.sql.Types.BIGINT);
    expect(cstmt.execute()).andReturn(true);
    expect(cstmt.getLong(9)).andReturn(99L);
    expect(cstmt.wasNull()).andReturn(false);

    expect(connection.prepareCall("{call dbo.uspInsertMetricValue(?, ?, ?)}")).andReturn(cstmt);
    cstmt.setLong(1, 99L);
    cstmt.setNString(2, "name");
    cstmt.setNString(3, "1234");
    expect(cstmt.execute()).andReturn(true);

    // replay
    replay(configuration, connection, cstmt, record, metric);

    SqlServerSink sink = createInstance();

    sink.init(configuration);

    SQLServerDriver.setConnection(connection);

    sink.putMetrics(record);

    verify(configuration, connection, cstmt, record, metric);
}

From source file:org.apache.hadoop.metrics2.sink.SqlServerSinkHadoop2Test.java

@Override
@Test//w w  w.  j ava 2 s  .c  om
public void testPutMetrics() throws Exception {
    SubsetConfiguration configuration = createNiceMock(SubsetConfiguration.class);
    Connection connection = createNiceMock(Connection.class);
    CallableStatement cstmt = createNiceMock(CallableStatement.class);
    MetricsRecord record = createNiceMock(MetricsRecord.class);
    AbstractMetric metric = createNiceMock(AbstractMetric.class);

    // set expectations
    expect(configuration.getParent()).andReturn(null);
    expect(configuration.getPrefix()).andReturn("prefix");
    expect(configuration.getString("databaseUrl")).andReturn("url");

    expect(record.context()).andReturn("context");
    expect(record.name()).andReturn("typeName");
    expect(record.tags()).andReturn(new HashSet<MetricsTag>());
    expect(record.timestamp()).andReturn(9999L);

    expect(record.metrics()).andReturn(Collections.singleton(metric));

    expect(metric.name()).andReturn("name").anyTimes();
    expect(metric.value()).andReturn(1234);

    expect(connection.prepareCall("{call dbo.uspGetMetricRecord(?, ?, ?, ?, ?, ?, ?, ?, ?)}")).andReturn(cstmt);
    cstmt.setNString(1, "context");
    cstmt.setNString(2, "typeName");
    cstmt.setNString(eq(3), (String) anyObject());
    cstmt.setNString(eq(4), (String) anyObject());
    cstmt.setNString(eq(5), (String) anyObject());
    cstmt.setNString(6, "prefix");
    cstmt.setNString(7, "sourceName:prefix");
    cstmt.setLong(8, 9999L);
    cstmt.registerOutParameter(9, java.sql.Types.BIGINT);
    expect(cstmt.execute()).andReturn(true);
    expect(cstmt.getLong(9)).andReturn(99L);
    expect(cstmt.wasNull()).andReturn(false);

    expect(connection.prepareCall("{call dbo.uspInsertMetricValue(?, ?, ?)}")).andReturn(cstmt);
    cstmt.setLong(1, 99L);
    cstmt.setNString(2, "name");
    cstmt.setNString(3, "1234");
    expect(cstmt.execute()).andReturn(true);

    // replay
    replay(configuration, connection, cstmt, record, metric);

    SqlServerSink sink = createInstance();

    sink.init(configuration);

    SQLServerDriver.setConnection(connection);

    sink.putMetrics(record);

    verify(configuration, connection, cstmt, record, metric);
}

From source file:org.apache.hadoop.metrics2.sink.SqlServerSinkTest.java

@Test
public void testGetMetricRecordID() throws Exception {
    SubsetConfiguration configuration = createNiceMock(SubsetConfiguration.class);
    Connection connection = createNiceMock(Connection.class);
    CallableStatement cstmt = createNiceMock(CallableStatement.class);

    // set expectations
    expect(configuration.getParent()).andReturn(null);
    expect(configuration.getPrefix()).andReturn("prefix");
    expect(configuration.getString("databaseUrl")).andReturn("url");

    expect(connection.prepareCall("{call dbo.uspGetMetricRecord(?, ?, ?, ?, ?, ?, ?, ?, ?)}")).andReturn(cstmt);
    cstmt.setNString(1, "context");
    cstmt.setNString(2, "typeName");
    cstmt.setNString(3, "nodeName");
    cstmt.setNString(4, "ip");
    cstmt.setNString(5, "clusterName");
    cstmt.setNString(6, "serviceName");
    cstmt.setNString(7, "tagPairs");
    cstmt.setLong(8, 9999L);/*w  ww .ja v  a2  s .co  m*/
    cstmt.registerOutParameter(9, java.sql.Types.BIGINT);
    expect(cstmt.execute()).andReturn(true);
    expect(cstmt.getLong(9)).andReturn(99L);
    expect(cstmt.wasNull()).andReturn(false);

    // replay
    replay(configuration, connection, cstmt);

    SqlServerSink sink = createInstance();

    sink.init(configuration);

    SQLServerDriver.setConnection(connection);

    Assert.assertEquals(99, sink.getMetricRecordID("context", "typeName", "nodeName", "ip", "clusterName",
            "serviceName", "tagPairs", 9999L));

    verify(configuration, connection, cstmt);
}

From source file:org.apache.hadoop.metrics2.sink.SqlServerSinkTest.java

@Test
public void testGetMetricRecordID_nullReturn() throws Exception {
    SubsetConfiguration configuration = createNiceMock(SubsetConfiguration.class);
    Connection connection = createNiceMock(Connection.class);
    CallableStatement cstmt = createNiceMock(CallableStatement.class);

    // set expectations
    expect(configuration.getParent()).andReturn(null);
    expect(configuration.getPrefix()).andReturn("prefix");
    expect(configuration.getString("databaseUrl")).andReturn("url");

    expect(connection.prepareCall("{call dbo.uspGetMetricRecord(?, ?, ?, ?, ?, ?, ?, ?, ?)}")).andReturn(cstmt);
    cstmt.setNString(1, "context");
    cstmt.setNString(2, "typeName");
    cstmt.setNString(3, "nodeName");
    cstmt.setNString(4, "ip");
    cstmt.setNString(5, "clusterName");
    cstmt.setNString(6, "serviceName");
    cstmt.setNString(7, "tagPairs");
    cstmt.setLong(8, 9999L);/*from w w w. ja v  a2s  .c om*/
    cstmt.registerOutParameter(9, java.sql.Types.BIGINT);
    expect(cstmt.execute()).andReturn(true);
    expect(cstmt.getLong(9)).andReturn(99L);
    expect(cstmt.wasNull()).andReturn(true);

    // replay
    replay(configuration, connection, cstmt);

    SqlServerSink sink = createInstance();

    sink.init(configuration);

    SQLServerDriver.setConnection(connection);

    Assert.assertEquals(-1, sink.getMetricRecordID("context", "typeName", "nodeName", "ip", "clusterName",
            "serviceName", "tagPairs", 9999L));

    verify(configuration, connection, cstmt);
}

From source file:org.apache.hadoop.metrics2.sink.SqlSink.java

public long getMetricRecordID(String recordTypeContext, String recordTypeName, String nodeName, String sourceIP,
        String clusterName, String serviceName, String tagPairs, long recordTimestamp) {
    CallableStatement cstmt = null;
    long result;//from ww w . j a  v  a  2s  .c  o  m
    logger.trace("Params: recordTypeContext = " + recordTypeContext + ", recordTypeName = " + recordTypeName
            + ", nodeName = " + nodeName + ", sourceIP = " + sourceIP + ", tagPairs = " + tagPairs
            + ", clusterName = " + clusterName + ", serviceName = " + serviceName + ", recordTimestamp = "
            + recordTimestamp);
    if (recordTypeContext == null || recordTypeName == null || nodeName == null || sourceIP == null
            || tagPairs == null)
        return -1;

    int colid = 1;
    try {
        if (ensureConnection()) {
            String procedureCall = String.format("{call %s(?, ?, ?, ?, ?, ?, ?, ?, ?)}",
                    getGetMetricsProcedureName());
            cstmt = conn.prepareCall(procedureCall);
            cstmt.setNString(colid++, recordTypeContext);
            cstmt.setNString(colid++, recordTypeName);
            cstmt.setNString(colid++, nodeName);
            cstmt.setNString(colid++, sourceIP);
            cstmt.setNString(colid++, clusterName);
            cstmt.setNString(colid++, serviceName);
            cstmt.setNString(colid++, tagPairs);
            cstmt.setLong(colid++, recordTimestamp);
            cstmt.registerOutParameter(colid, java.sql.Types.BIGINT);
            cstmt.execute();

            result = cstmt.getLong(colid);
            if (cstmt.wasNull())
                return -1;
            return result;
        }
    } catch (Exception e) {
        if (DEBUG)
            logger.info("Error during getMetricRecordID call sproc: " + e.toString());
        flush();
    } finally {
        if (cstmt != null) {
            try {
                cstmt.close();
            } catch (SQLException se) {
                if (DEBUG)
                    logger.info("Error during getMetricRecordID close cstmt: " + se.toString());
            }
            /*
             * We don't close the connection here because we are likely to be
             * writing
             * metric values next and it is more efficient to share the connection.
             */
        }
    }
    return -1;
}

From source file:org.nuclos.server.dblayer.impl.standard.StandardSqlDBAccess.java

protected <T> T getCallableResultValue(CallableStatement stmt, int index, Class<T> javaType)
        throws SQLException {
    Object value;//from   ww w .ja  v  a 2 s.c  o  m
    if (javaType == String.class) {
        value = stmt.getString(index);
    } else if (javaType == NuclosPassword.class) {
        value = new NuclosPassword(ServerCryptUtil.decrypt(stmt.getString(index)));
    } else if (javaType == Double.class) {
        value = stmt.getDouble(index);
    } else if (javaType == Long.class) {
        value = stmt.getLong(index);
    } else if (javaType == Integer.class) {
        value = stmt.getInt(index);
    } else if (javaType == Boolean.class) {
        value = stmt.getBoolean(index);
    } else if (javaType == BigDecimal.class) {
        value = stmt.getBigDecimal(index);
    } else if (javaType == java.util.Date.class) {
        value = stmt.getDate(index);
    } else if (javaType == byte[].class) {
        value = stmt.getBytes(index);
    } else if (javaType == NuclosScript.class) {
        final XStreamSupport xs = XStreamSupport.getInstance();
        final XStream xstream = xs.getXStream();
        try {
            value = xstream.fromXML(stmt.getString(index));
        } finally {
            xs.returnXStream(xstream);
        }
    } else {
        throw new IllegalArgumentException("Class " + javaType + " not supported by readField");
    }
    return stmt.wasNull() ? null : javaType.cast(value);
}