Example usage for java.sql CallableStatement setNString

List of usage examples for java.sql CallableStatement setNString

Introduction

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

Prototype

void setNString(String parameterName, String value) throws SQLException;

Source Link

Document

Sets the designated parameter to the given String object.

Usage

From source file:it.greenvulcano.gvesb.datahandling.dbo.DBOCallSP.java

private void setNString(CallableStatement cs, String text) throws SQLException {
    if (useName) {
        cs.setNString(currName, text);
    } else {/*from ww w. j  a va  2  s .  co  m*/
        cs.setNString(colIdx, text);
    }
}

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;/* www .  jav a  2  s  .  com*/
    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.SqlServerSink.java

public void insertMetricValue(long metricRecordID, String metricName, String metricValue) {
    CallableStatement cstmt = null;
    if (metricRecordID < 0 || metricName == null || metricValue == null)
        return;/*ww w. j  a  v a  2  s .  c  o  m*/
    try {
        if (ensureConnection()) {
            cstmt = conn.prepareCall("{call dbo.uspInsertMetricValue(?, ?, ?)}");
            cstmt.setLong(1, metricRecordID);
            cstmt.setNString(2, metricName);
            cstmt.setNString(3, metricValue);
            cstmt.execute();
        }
    } catch (Exception e) {
        if (DEBUG)
            logger.info("Error during insertMetricValue call sproc: " + e.toString());
        flush();
    } finally {
        if (cstmt != null) {
            try {
                cstmt.close();
            } catch (SQLException se) {
                if (DEBUG)
                    logger.info("Error during insertMetricValue close cstmt: " + se.toString());
            }
            /*
             * We don't close the connection here because we are likely to be
             * writing
             * more metric values next and it is more efficient to share the
             * connection.
             */
        }
    }
}

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

@Override
@Test/*from  w  ww .j a va  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/*ww  w  .j  a  v  a2s . co 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);
    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  .  j  a v  a  2s.c om*/
    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);//  w w  w  .  j a  v a2s  .  c  o m
    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.SqlServerSinkTest.java

@Test
public void testInsertMetricValue() 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.uspInsertMetricValue(?, ?, ?)}")).andReturn(cstmt);
    cstmt.setLong(1, 9999L);//from w w w.j a  va2s  .c  om
    cstmt.setNString(2, "metricName");
    cstmt.setNString(3, "metricValue");
    expect(cstmt.execute()).andReturn(true);

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

    SqlServerSink sink = createInstance();

    sink.init(configuration);

    SQLServerDriver.setConnection(connection);

    sink.insertMetricValue(9999L, "metricName", "metricValue");

    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 w w w . j a v a 2  s.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.apache.hadoop.metrics2.sink.SqlSink.java

public void insertMetricValue(long metricRecordID, String metricName, String metricValue) {
    CallableStatement cstmt = null;
    if (metricRecordID < 0 || metricName == null || metricValue == null)
        return;//w ww . j  a v a2s  .c o  m
    try {
        logger.trace("Insert metricRecordId : " + metricRecordID + ", " + "metricName : " + metricName
                + ", metricValue : " + metricValue + ", " + "procedure = " + getInsertMetricsProcedureName());
        if (ensureConnection()) {
            String procedureCall = String.format("{call %s(?, ?, ?)}", getInsertMetricsProcedureName());
            cstmt = conn.prepareCall(procedureCall);
            cstmt.setLong(1, metricRecordID);
            cstmt.setNString(2, metricName);
            cstmt.setNString(3, metricValue);
            cstmt.execute();
        }
    } catch (Exception e) {
        if (DEBUG)
            logger.info("Error during insertMetricValue call sproc: " + e.toString());
        flush();
    } finally {
        if (cstmt != null) {
            try {
                cstmt.close();
            } catch (SQLException se) {
                if (DEBUG)
                    logger.info("Error during insertMetricValue close cstmt: " + se.toString());
            }
            /*
             * We don't close the connection here because we are likely to be
             * writing
             * more metric values next and it is more efficient to share the
             * connection.
             */
        }
    }
}