Search in sources :

Example 31 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testBigInt.

/**
 * Test with bigint value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testBigInt() throws SQLException {
    Random r = new Random();
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    String[] numeric = createNumericValues();
    tvp.addRow(Long.parseLong(numeric[4]));
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    if (null != pstmt) {
        pstmt.close();
    }
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTable);
    while (rs.next()) {
        assertEquals(rs.getLong(1), Long.parseLong(numeric[4]));
    }
}
Also used : Random(java.util.Random) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 32 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testDateTime.

/**
 * Test ith datetime
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testDateTime() throws SQLException {
    java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.0");
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    tvp.addRow(timestamp);
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    if (null != pstmt) {
        pstmt.close();
    }
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTable);
    while (rs.next()) {
        assertEquals(rs.getString(1), "" + timestamp);
    // System.out.println(rs.getDateTime(1));// TODO does not work
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 33 with SQLServerDataTable

use of com.microsoft.sqlserver.jdbc.SQLServerDataTable 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 34 with SQLServerDataTable

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

the class TVPWithSqlVariantTest method testNull.

/**
 * Test with null value
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
// TODO We need to check this later. Right now sending null with TVP is not supported
@Test
public void testNull() throws SQLException {
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.SQL_VARIANT);
    try {
        tvp.addRow((Date) null);
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Use of TVPs containing null sql_variant columns is not supported."));
    }
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + destTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    if (null != pstmt) {
        pstmt.close();
    }
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTable);
    while (rs.next()) {
        System.out.println(rs.getString(1));
    }
}
Also used : SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Aggregations

SQLServerDataTable (com.microsoft.sqlserver.jdbc.SQLServerDataTable)34 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)33 Test (org.junit.jupiter.api.Test)33 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)13 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)7 SQLServerResultSet (com.microsoft.sqlserver.jdbc.SQLServerResultSet)4 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)3 SQLTimeoutException (java.sql.SQLTimeoutException)2 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)1 SqlDate (com.microsoft.sqlserver.testframework.sqlType.SqlDate)1 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)1 BigDecimal (java.math.BigDecimal)1 Date (java.sql.Date)1 Random (java.util.Random)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1