Search in sources :

Example 46 with SQLServerCallableStatement

use of com.microsoft.sqlserver.jdbc.SQLServerCallableStatement in project mssql-jdbc by Microsoft.

the class CallableStatementTest method testOutputProcedure3Inorder.

private void testOutputProcedure3Inorder(String sql) throws SQLException {
    try (SQLServerCallableStatement callableStatement = (SQLServerCallableStatement) Util.getCallableStmt(con, sql, stmtColEncSetting)) {
        callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
        callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
        callableStatement.execute();
        int intValue = callableStatement.getInt(1);
        assertEquals("" + intValue, numericValues[3], "Test for output parameter fails.\n");
        int intValue2 = callableStatement.getInt(2);
        assertEquals("" + intValue2, numericValues[3], "Test for output parameter fails.\n");
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLException(java.sql.SQLException)

Example 47 with SQLServerCallableStatement

use of com.microsoft.sqlserver.jdbc.SQLServerCallableStatement in project mssql-jdbc by Microsoft.

the class CallableStatementTest method testMixedProcedure2Inorder.

private void testMixedProcedure2Inorder(String sql) throws SQLException {
    try (SQLServerCallableStatement callableStatement = (SQLServerCallableStatement) Util.getCallableStmt(con, sql, stmtColEncSetting)) {
        callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
        callableStatement.registerOutParameter(2, java.sql.Types.FLOAT);
        callableStatement.setInt(3, Integer.parseInt(numericValues[3]));
        callableStatement.setDouble(4, Double.parseDouble(numericValues[5]));
        callableStatement.execute();
        int intValue = callableStatement.getInt(1);
        assertEquals("" + intValue, numericValues[3], "Test for output parameter fails.\n");
        double floatValue = callableStatement.getDouble(2);
        assertEquals("" + floatValue, numericValues[5], "Test for output parameter fails.\n");
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLException(java.sql.SQLException)

Example 48 with SQLServerCallableStatement

use of com.microsoft.sqlserver.jdbc.SQLServerCallableStatement in project mssql-jdbc by Microsoft.

the class TVPWithSqlVariantTest method testIntStoredProcedure.

/**
 * Test with stored procedure
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testIntStoredProcedure() throws SQLException {
    java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.0");
    final String sql = "{call " + procedureName + "(?)}";
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    tvp.addRow(timestamp);
    SQLServerCallableStatement Cstatement = (SQLServerCallableStatement) connection.prepareCall(sql);
    Cstatement.setStructured(1, tvpName, tvp);
    Cstatement.execute();
    rs = (SQLServerResultSet) stmt.executeQuery("select * from " + destTable);
    while (rs.next()) {
        System.out.println(rs.getString(1));
    }
    if (null != Cstatement) {
        Cstatement.close();
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 49 with SQLServerCallableStatement

use of com.microsoft.sqlserver.jdbc.SQLServerCallableStatement in project mssql-jdbc by Microsoft.

the class WrapperTest method wrapTest.

/**
 * Wrapper tests
 * @throws Exception
 */
@Test
public void wrapTest() throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection con = DriverManager.getConnection(connectionString);
    Statement stmt = con.createStatement();
    try {
        // First make sure that a statement can be unwrapped
        boolean isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
        assertEquals(isWrapper, true, "SQLServerStatement should be a wrapper for self");
        isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerStatement"));
        assertEquals(isWrapper, true, "SQLServerStatement should be a wrapper for ISQLServerStatement");
        isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnection"));
        assertEquals(isWrapper, false, "SQLServerStatement should not be a wrapper for SQLServerConnection");
        // Now make sure that we can unwrap a SQLServerCallableStatement to a SQLServerStatement
        CallableStatement cs = con.prepareCall("{  ? = CALL " + "ProcName" + " (?, ?, ?, ?) }");
        // Test the class first
        isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
        assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for SQLServerStatement");
        // Now unwrap the Callable to a statement and call a SQLServerStatement specific function and make sure it succeeds.
        SQLServerStatement stmt2 = (SQLServerStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
        stmt2.setResponseBuffering("adaptive");
        // now test the interface
        isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerCallableStatement"));
        assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for ISQLServerCallableStatement");
        // Now unwrap the Callable to a statement and call a SQLServerStatement specific function and make sure it succeeds.
        ISQLServerPreparedStatement stmt4 = (ISQLServerPreparedStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement"));
        stmt4.setResponseBuffering("adaptive");
        if (isKatmaiServer())
            stmt4.setDateTimeOffset(1, null);
        // Try Unwrapping CallableStatement to a callableStatement
        isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerCallableStatement"));
        assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for SQLServerCallableStatement");
        // Now unwrap the Callable to a SQLServerCallableStatement and call a SQLServerStatement specific function and make sure it succeeds.
        SQLServerCallableStatement stmt3 = (SQLServerCallableStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerCallableStatement"));
        stmt3.setResponseBuffering("adaptive");
        if (isKatmaiServer()) {
            stmt3.setDateTimeOffset(1, null);
        }
        if (null != stmt4) {
            stmt4.close();
        }
        if (null != cs) {
            cs.close();
        }
    } catch (UnsupportedOperationException e) {
        assertEquals(System.getProperty("java.specification.version"), "1.5", "isWrapperFor should be supported in anything other than 1.5");
        assertTrue(e.getMessage().equalsIgnoreCase("This operation is not supported."), "Wrong exception message");
    } finally {
        if (null != stmt) {
            stmt.close();
        }
        if (null != con) {
            con.close();
        }
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ISQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) ISQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 50 with SQLServerCallableStatement

use of com.microsoft.sqlserver.jdbc.SQLServerCallableStatement in project mssql-jdbc by Microsoft.

the class CallableStatementTest method getSetNullWithTypeVarchar.

/**
 * test for setNull(index, varchar) to behave as setNull(index, nvarchar) when SendStringParametersAsUnicode is true
 *
 * @throws SQLException
 */
@Test
public void getSetNullWithTypeVarchar() throws SQLException {
    String polishchar = "\u0143";
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setURL(connectionString);
    ds.setSendStringParametersAsUnicode(true);
    String sql = "{? = call " + setNullProcedureName + " (?,?)}";
    try (Connection connection = ds.getConnection();
        SQLServerCallableStatement cs = (SQLServerCallableStatement) connection.prepareCall(sql);
        SQLServerCallableStatement cs2 = (SQLServerCallableStatement) connection.prepareCall(sql)) {
        cs.registerOutParameter(1, Types.INTEGER);
        cs.setString(2, polishchar);
        cs.setString(3, null);
        cs.registerOutParameter(3, Types.VARCHAR);
        cs.execute();
        String expected = cs.getString(3);
        cs2.registerOutParameter(1, Types.INTEGER);
        cs2.setString(2, polishchar);
        cs2.setNull(3, Types.VARCHAR);
        cs2.registerOutParameter(3, Types.NVARCHAR);
        cs2.execute();
        String actual = cs2.getString(3);
        assertEquals(expected, actual);
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)51 SQLException (java.sql.SQLException)35 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)17 Test (org.junit.jupiter.api.Test)17 BigDecimal (java.math.BigDecimal)12 SQLServerDataTable (com.microsoft.sqlserver.jdbc.SQLServerDataTable)7 Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 CallableStatement (java.sql.CallableStatement)4 Statement (java.sql.Statement)4 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)3 DisplayName (org.junit.jupiter.api.DisplayName)3 SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)2 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)2 SQLServerResultSet (com.microsoft.sqlserver.jdbc.SQLServerResultSet)2 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)2 PreparedStatement (java.sql.PreparedStatement)2 Properties (java.util.Properties)2 ISQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement)1 SQLServerCallableStatement42 (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement42)1