Java Code Examples for java.sql.CallableStatement#setTime()

The following examples show how to use java.sql.CallableStatement#setTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Calls a SQL procedure that takes non-numeric IN and OUT parameters.
 * @throws SQLException 
 */
public void testNonNumericTypesInAndOutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call NON_NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?)");

    cs.setDate(1, Date.valueOf("2002-05-12"));
    cs.setTime(2, Time.valueOf("10:05:02"));
    cs.setTimestamp(3, Timestamp.valueOf("2002-05-12 10:05:02.000000000"));
    byte[] ba = new byte[2];
    ba[0] = 1;
    ba[1] = 2;
    cs.setBytes(4, ba);

    cs.registerOutParameter (5, java.sql.Types.DATE);
    cs.registerOutParameter (6, java.sql.Types.TIME);
    cs.registerOutParameter (7, java.sql.Types.TIMESTAMP);
    cs.registerOutParameter (8, java.sql.Types.VARBINARY);

    cs.execute();

    assertEquals("OUT date", Date.valueOf("2002-05-12"), cs.getDate(5));
    assertEquals("OUT time"  , Time.valueOf("10:05:02"), cs.getTime(6));
    assertEquals("OUT timestamp" , 
        Timestamp.valueOf("2002-05-12 10:05:02.000000000"), 
        cs.getTimestamp(7));
    assertTrue(Arrays.equals(ba, cs.getBytes(8)));
}
 
Example 2
Source File: SWCallableStatementTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void testBatch() throws SQLException, MalformedURLException {
    CallableStatement preparedStatement = multiHostConnection.prepareCall("UPDATE test SET a = ? WHERE b = ?");
    preparedStatement.setShort(1, (short) 12);
    preparedStatement.setTime(2, new Time(System.currentTimeMillis()));
    preparedStatement.addBatch();
    int[] resultSet = preparedStatement.executeBatch();
    preparedStatement.clearBatch();

    verify(mysqlCallableStatement).executeBatch();
    verify(mysqlCallableStatement).addBatch();
    verify(mysqlCallableStatement).clearBatch();

    assertThat(segmentStorage.getTraceSegments().size(), is(1));
    TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
    assertThat(spans.size(), is(1));
    assertDBSpan(spans.get(0), "Mysql/JDBI/CallableStatement/executeBatch", "");
}
 
Example 3
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes non-numeric IN and OUT parameters.
 * @throws SQLException 
 */
public void testNonNumericTypesInAndOutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call NON_NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?)");

    cs.setDate(1, Date.valueOf("2002-05-12"));
    cs.setTime(2, Time.valueOf("10:05:02"));
    cs.setTimestamp(3, Timestamp.valueOf("2002-05-12 10:05:02.000000000"));
    byte[] ba = new byte[2];
    ba[0] = 1;
    ba[1] = 2;
    cs.setBytes(4, ba);

    cs.registerOutParameter (5, java.sql.Types.DATE);
    cs.registerOutParameter (6, java.sql.Types.TIME);
    cs.registerOutParameter (7, java.sql.Types.TIMESTAMP);
    cs.registerOutParameter (8, java.sql.Types.VARBINARY);

    cs.execute();

    assertEquals("OUT date", Date.valueOf("2002-05-12"), cs.getDate(5));
    assertEquals("OUT time"  , Time.valueOf("10:05:02"), cs.getTime(6));
    assertEquals("OUT timestamp" , 
        Timestamp.valueOf("2002-05-12 10:05:02.000000000"), 
        cs.getTimestamp(7));
    assertTrue(Arrays.equals(ba, cs.getBytes(8)));
}
 
Example 4
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes INOUT parameters of various types.
 * @throws SQLException 
 */
public void testManyTypesInoutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call MANY_TYPES_INOUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.registerOutParameter (2, java.sql.Types.SMALLINT);
    cs.registerOutParameter (4, java.sql.Types.INTEGER);
    cs.registerOutParameter (6, java.sql.Types.BIGINT);
    cs.registerOutParameter (8, java.sql.Types.REAL);
    cs.registerOutParameter (10, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.TIME);

    cs.setShort(1, (short)6);
    cs.setShort(2, (short)9);
    cs.setInt(3, 6);
    cs.setInt(4, 9);
    cs.setLong(5, (long)99999);
    cs.setLong(6, (long)88888888);
    cs.setFloat(7, (float)6.123453);
    cs.setFloat(8, (float)77777);
    cs.setDouble(9, (double)6.123453);
    cs.setDouble(10, (double)8888888888888.01234);
    cs.setTime(11, Time.valueOf("11:06:03"));
    cs.setTime(12, Time.valueOf("10:05:02"));

    cs.execute();

    assertEquals("Short: Sum of 6 + 9", 15, cs.getShort(2));
    assertEquals("Int: Sum of 6 + 9", 15, cs.getInt(4));
    assertEquals("Long: Sum of 99999 + 88888888", 88988887, cs.getLong(6));
    assertEquals("Float: Sum of 6.123453 and 77777" , (float) 77783.123453,
        cs.getFloat(8), .000001);
    assertEquals("Double: Sum of Sum of 6.987654 and 8888888888888.01234",
        8.888888888894135e12, cs.getDouble(10), .000001);
    assertEquals("Time: changed to", Time.valueOf("11:06:03"), 
        cs.getTime(12));

}
 
Example 5
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes INOUT parameters of various types.
 * @throws SQLException 
 */
public void testManyTypesInoutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call MANY_TYPES_INOUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.registerOutParameter (2, java.sql.Types.SMALLINT);
    cs.registerOutParameter (4, java.sql.Types.INTEGER);
    cs.registerOutParameter (6, java.sql.Types.BIGINT);
    cs.registerOutParameter (8, java.sql.Types.REAL);
    cs.registerOutParameter (10, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.TIME);

    cs.setShort(1, (short)6);
    cs.setShort(2, (short)9);
    cs.setInt(3, 6);
    cs.setInt(4, 9);
    cs.setLong(5, (long)99999);
    cs.setLong(6, (long)88888888);
    cs.setFloat(7, (float)6.123453);
    cs.setFloat(8, (float)77777);
    cs.setDouble(9, (double)6.123453);
    cs.setDouble(10, (double)8888888888888.01234);
    cs.setTime(11, Time.valueOf("11:06:03"));
    cs.setTime(12, Time.valueOf("10:05:02"));

    cs.execute();

    assertEquals("Short: Sum of 6 + 9", 15, cs.getShort(2));
    assertEquals("Int: Sum of 6 + 9", 15, cs.getInt(4));
    assertEquals("Long: Sum of 99999 + 88888888", 88988887, cs.getLong(6));
    assertEquals("Float: Sum of 6.123453 and 77777" , (float) 77783.123453,
        cs.getFloat(8), .000001);
    assertEquals("Double: Sum of Sum of 6.987654 and 8888888888888.01234",
        8.888888888894135e12, cs.getDouble(10), .000001);
    assertEquals("Time: changed to", Time.valueOf("11:06:03"), 
        cs.getTime(12));

}
 
Example 6
Source File: CallableTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes non-numeric IN and OUT parameters.
 * @throws SQLException 
 */
public void testNonNumericTypesInAndOutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call NON_NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?)");

    cs.setDate(1, Date.valueOf("2002-05-12"));
    cs.setTime(2, Time.valueOf("10:05:02"));
    cs.setTimestamp(3, Timestamp.valueOf("2002-05-12 10:05:02.000000000"));
    byte[] ba = new byte[2];
    ba[0] = 1;
    ba[1] = 2;
    cs.setBytes(4, ba);

    cs.registerOutParameter (5, java.sql.Types.DATE);
    cs.registerOutParameter (6, java.sql.Types.TIME);
    cs.registerOutParameter (7, java.sql.Types.TIMESTAMP);
    cs.registerOutParameter (8, java.sql.Types.VARBINARY);

    cs.execute();

    assertEquals("OUT date", Date.valueOf("2002-05-12"), cs.getDate(5));
    assertEquals("OUT time"  , Time.valueOf("10:05:02"), cs.getTime(6));
    assertEquals("OUT timestamp" , 
        Timestamp.valueOf("2002-05-12 10:05:02.000000000"), 
        cs.getTimestamp(7));
    assertTrue(Arrays.equals(ba, cs.getBytes(8)));
}
 
Example 7
Source File: CallableTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Private helper for {@link #testTimeAndDateWithCalendar()}. This method
 * calls a procedure that takes Date, Time and Timestamp arguments and
 * returns the exact same values. Call the setters with one calendar and
 * the getters with another calendar, and verify that the expected
 * conversion between time zones has happened.
 *
 * @param cal1 the calendar to use for the setter methods
 * @param cal2 the calendar to use for the getter methods
 */
private void testTimeAndDateWithCalendar(Calendar cal1, Calendar cal2)
        throws SQLException
{
    println("Running " + getName() + "() with " +
            cal1.getTimeZone().getDisplayName() + " and " +
            cal2.getTimeZone().getDisplayName());

    CallableStatement cs = prepareCall(
            "call NON_NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?)");

    Date d = Date.valueOf("2010-04-14");
    Time t = Time.valueOf("12:23:24");
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    ts.setNanos(123456789);

    cs.setDate(1, d, cal1);
    cs.setTime(2, t, cal1);
    cs.setTimestamp(3, ts, cal1);
    cs.setNull(4, Types.VARBINARY); // we don't care about VARBINARY here

    cs.registerOutParameter (5, java.sql.Types.DATE);
    cs.registerOutParameter (6, java.sql.Types.TIME);
    cs.registerOutParameter (7, java.sql.Types.TIMESTAMP);
    cs.registerOutParameter (8, java.sql.Types.VARBINARY);

    cs.execute();

    assertSameDate(d, cal1, cs.getDate(5, cal2), cal2);
    assertSameTime(t, cal1, cs.getTime(6, cal2), cal2);
    assertSameTimestamp(ts, cal1, cs.getTimestamp(7, cal2), cal2);
}
 
Example 8
Source File: CallableTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes INOUT parameters of various types.
 * @throws SQLException 
 */
public void testManyTypesInoutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call MANY_TYPES_INOUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.registerOutParameter (2, java.sql.Types.SMALLINT);
    cs.registerOutParameter (4, java.sql.Types.INTEGER);
    cs.registerOutParameter (6, java.sql.Types.BIGINT);
    cs.registerOutParameter (8, java.sql.Types.REAL);
    cs.registerOutParameter (10, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.TIME);

    cs.setShort(1, (short)6);
    cs.setShort(2, (short)9);
    cs.setInt(3, 6);
    cs.setInt(4, 9);
    cs.setLong(5, (long)99999);
    cs.setLong(6, (long)88888888);
    cs.setFloat(7, (float)6.123453);
    cs.setFloat(8, (float)77777);
    cs.setDouble(9, (double)6.123453);
    cs.setDouble(10, (double)8888888888888.01234);
    cs.setTime(11, Time.valueOf("11:06:03"));
    cs.setTime(12, Time.valueOf("10:05:02"));

    cs.execute();

    assertEquals("Short: Sum of 6 + 9", 15, cs.getShort(2));
    assertEquals("Int: Sum of 6 + 9", 15, cs.getInt(4));
    assertEquals("Long: Sum of 99999 + 88888888", 88988887, cs.getLong(6));
    assertEquals("Float: Sum of 6.123453 and 77777" , (float) 77783.123453,
        cs.getFloat(8), .000001);
    assertEquals("Double: Sum of Sum of 6.987654 and 8888888888888.01234",
        8.888888888894135e12, cs.getDouble(10), .000001);
    assertEquals("Time: changed to", Time.valueOf("11:06:03"), 
        cs.getTime(12));

}
 
Example 9
Source File: BatchUpdateTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testCallableStatementBatch() throws SQLException {

        println("Positive Callable Statement: " +
            "statement testing callable statement batch");
        CallableStatement cs = prepareCall("insert into t1 values(?)");

        cs.setInt(1, 1);
        cs.addBatch();
        cs.setInt(1,2);
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "t1");

        /* For 'beetle' bug 2813 - setDate/setTime/setTimestamp
         * calls on callableStatement throws ClassNotFoundException
         * verify setXXXX() works with Date, Time and Timestamp
         * on CallableStatement.
         */
        cs = prepareCall("insert into datetab values(?)");

        cs.setDate(1, Date.valueOf("1990-05-05"));
        cs.addBatch();
        cs.setDate(1,Date.valueOf("1990-06-06"));
        cs.addBatch();

        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "datetab");

        cs = prepareCall("insert into timetab values(?)");

        cs.setTime(1, Time.valueOf("11:11:11"));
        cs.addBatch();
        cs.setTime(1, Time.valueOf("12:12:12"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "timestamptab");

        cs = prepareCall("insert into timestamptab values(?)");

        cs.setTimestamp(1, Timestamp.valueOf("1990-05-05 11:11:11.1"));
        cs.addBatch();
        cs.setTimestamp(1, Timestamp.valueOf("1992-07-07 12:12:12.2"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "timestamptab");

        // Try with a user type
        cs = prepareCall("insert into usertypetab values(?)");

        cs.setObject(1, Date.valueOf("1990-05-05"));
        cs.addBatch();
        cs.setObject(1,Date.valueOf("1990-06-06"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "usertypetab");
    }
 
Example 10
Source File: BatchUpdateTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testCallableStatementBatch() throws SQLException {

        println("Positive Callable Statement: " +
            "statement testing callable statement batch");
        CallableStatement cs = prepareCall("insert into t1 values(?)");

        cs.setInt(1, 1);
        cs.addBatch();
        cs.setInt(1,2);
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "t1");

        /* For 'beetle' bug 2813 - setDate/setTime/setTimestamp
         * calls on callableStatement throws ClassNotFoundException
         * verify setXXXX() works with Date, Time and Timestamp
         * on CallableStatement.
         */
        cs = prepareCall("insert into datetab values(?)");

        cs.setDate(1, Date.valueOf("1990-05-05"));
        cs.addBatch();
        cs.setDate(1,Date.valueOf("1990-06-06"));
        cs.addBatch();

        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "datetab");

        cs = prepareCall("insert into timetab values(?)");

        cs.setTime(1, Time.valueOf("11:11:11"));
        cs.addBatch();
        cs.setTime(1, Time.valueOf("12:12:12"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "timestamptab");

        cs = prepareCall("insert into timestamptab values(?)");

        cs.setTimestamp(1, Timestamp.valueOf("1990-05-05 11:11:11.1"));
        cs.addBatch();
        cs.setTimestamp(1, Timestamp.valueOf("1992-07-07 12:12:12.2"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "timestamptab");

        // Try with a user type
        cs = prepareCall("insert into usertypetab values(?)");

        cs.setObject(1, Date.valueOf("1990-05-05"));
        cs.addBatch();
        cs.setObject(1,Date.valueOf("1990-06-06"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "usertypetab");
    }
 
Example 11
Source File: JDBCExecutor.java    From amforeas with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Utility method which registers in a CallableStatement object the different {@link amforeas.jdbc.StoredProcedureParam}
 * instances in the given list. Returns a List of {@link amforeas.jdbc.StoredProcedureParam} with all the OUT parameters
 * registered in the CallableStatement
 * @param cs the CallableStatement object where the parameters are registered.
 * @param params a list of {@link amforeas.jdbc.StoredProcedureParam}
 * @return a list of OUT {@link amforeas.jdbc.StoredProcedureParam} 
 * @throws SQLException if we fail to register any of the parameters in the CallableStatement
 * @throws AmforeasBadRequestException 
 */
private List<StoredProcedureParam> addParameters (final CallableStatement cs, final List<StoredProcedureParam> params) throws SQLException, AmforeasBadRequestException {
    final List<StoredProcedureParam> outParams = new ArrayList<StoredProcedureParam>();
    int i = 1;
    for (StoredProcedureParam p : params) {
        final Integer sqlType = p.getSqlType();
        if (p.isOutParameter()) {
            l.debug("Adding OUT parameter " + p.toString());
            cs.registerOutParameter(i++, sqlType);
            outParams.add(p);
        } else {
            l.debug("Adding IN parameter " + p.toString());
            switch (sqlType) {
                case Types.BIGINT:
                case Types.INTEGER:
                case Types.TINYINT:
                    // case Types.NUMERIC:
                    cs.setInt(i++, Integer.valueOf(p.getValue()));
                    break;
                case Types.DATE:
                    cs.setDate(i++, (Date) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.TIME:
                    cs.setTime(i++, (Time) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.TIMESTAMP:
                    cs.setTimestamp(i++, (Timestamp) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.DECIMAL:
                    cs.setBigDecimal(i++, (BigDecimal) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.DOUBLE:
                    cs.setDouble(i++, Double.valueOf(p.getValue()));
                    break;
                case Types.FLOAT:
                    cs.setLong(i++, Long.valueOf(p.getValue()));
                    break;
                default:
                    cs.setString(i++, p.getValue());
                    break;
            }
        }
    }
    return outParams;
}
 
Example 12
Source File: BatchUpdateTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testCallableStatementBatch() throws SQLException {

        println("Positive Callable Statement: " +
            "statement testing callable statement batch");
        CallableStatement cs = prepareCall("insert into t1 values(?)");

        cs.setInt(1, 1);
        cs.addBatch();
        cs.setInt(1,2);
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "t1");

        /* For 'beetle' bug 2813 - setDate/setTime/setTimestamp
         * calls on callableStatement throws ClassNotFoundException 
         * verify setXXXX() works with Date, Time and Timestamp 
         * on CallableStatement.
         */
        cs = prepareCall("insert into datetab values(?)");

        cs.setDate(1, Date.valueOf("1990-05-05"));
        cs.addBatch();
        cs.setDate(1,Date.valueOf("1990-06-06"));
        cs.addBatch();

        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "datetab");

        cs = prepareCall("insert into timetab values(?)");

        cs.setTime(1, Time.valueOf("11:11:11"));
        cs.addBatch();
        cs.setTime(1, Time.valueOf("12:12:12"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "timestamptab");

        cs = prepareCall("insert into timestamptab values(?)");

        cs.setTimestamp(1, Timestamp.valueOf("1990-05-05 11:11:11.1"));
        cs.addBatch();
        cs.setTimestamp(1, Timestamp.valueOf("1992-07-07 12:12:12.2"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "timestamptab");

        // Try with a user type
        cs = prepareCall("insert into usertypetab values(?)");

        cs.setObject(1, Date.valueOf("1990-05-05"));
        cs.addBatch();
        cs.setObject(1,Date.valueOf("1990-06-06"));
        cs.addBatch();
        executeBatchCallableStatement(cs);

        cleanUpCallableStatement(cs, "usertypetab");
    }