Search in sources :

Example 36 with SQLServerPreparedStatement

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

the class TVPTypesTest method testImage.

/**
 * Test text support
 *
 * @throws SQLException
 */
@Test
public void testImage() throws SQLException {
    createTables("varbinary(max)");
    createTVPS("varbinary(max)");
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 10000; i++) buffer.append("a");
    value = buffer.toString();
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY);
    tvp.addRow(value.getBytes());
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + table + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    Connection con = DriverManager.getConnection(connectionString);
    ResultSet rs = con.createStatement().executeQuery("select * from " + table);
    while (rs.next()) assertTrue(parseByte(rs.getBytes(1), value.getBytes()));
    if (null != pstmt) {
        pstmt.close();
    }
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) SQLServerResultSet(com.microsoft.sqlserver.jdbc.SQLServerResultSet) ResultSet(java.sql.ResultSet) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 37 with SQLServerPreparedStatement

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

the class CallableStatementTest method populateDateNormalCase.

private static void populateDateNormalCase() throws SQLException {
    String sql = "insert into " + dateTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";
    SQLServerPreparedStatement sqlPstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql, stmtColEncSetting);
    // date
    for (int i = 1; i <= 3; i++) {
        sqlPstmt.setDate(i, (Date) dateValues.get(0));
    }
    // datetime2 default
    for (int i = 4; i <= 6; i++) {
        sqlPstmt.setTimestamp(i, (Timestamp) dateValues.get(1));
    }
    // datetimeoffset default
    for (int i = 7; i <= 9; i++) {
        sqlPstmt.setDateTimeOffset(i, (DateTimeOffset) dateValues.get(2));
    }
    // time default
    for (int i = 10; i <= 12; i++) {
        sqlPstmt.setTime(i, (Time) dateValues.get(3));
    }
    // datetime2(2)
    for (int i = 13; i <= 15; i++) {
        sqlPstmt.setTimestamp(i, (Timestamp) dateValues.get(4), 2);
    }
    // time(2)
    for (int i = 16; i <= 18; i++) {
        sqlPstmt.setTime(i, (Time) dateValues.get(5), 2);
    }
    // datetimeoffset(2)
    for (int i = 19; i <= 21; i++) {
        sqlPstmt.setDateTimeOffset(i, (DateTimeOffset) dateValues.get(6), 2);
    }
    // datetime()
    for (int i = 22; i <= 24; i++) {
        sqlPstmt.setDateTime(i, (Timestamp) dateValues.get(7));
    }
    // smalldatetime()
    for (int i = 25; i <= 27; i++) {
        sqlPstmt.setSmallDateTime(i, (Timestamp) dateValues.get(8));
    }
    sqlPstmt.execute();
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)

Example 38 with SQLServerPreparedStatement

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

the class DateAndTimeTypeTest method testQueryDateTVP.

/**
 * Test query with date TVP
 */
@Test
public void testQueryDateTVP() throws SQLException {
    SQLServerDataTable tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.DATE);
    tvp.addRow(DATE_TO_TEST);
    String sPrepStmt = "select * from dateandtime where my_date IN (select * from ?)";
    pstmt = connection.prepareStatement(sPrepStmt);
    ((SQLServerPreparedStatement) pstmt).setStructured(1, "dateTVP", tvp);
    rs = pstmt.executeQuery();
    rs.next();
    assertTrue(rs.getInt(1) == 42, "did not find correct timestamp");
    rs.close();
    pstmt.close();
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 39 with SQLServerPreparedStatement

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

the class DateAndTimeTypeTest method testQueryTimeTVP.

/**
 * Test query with date TVP
 */
@Test
public void testQueryTimeTVP() throws SQLException {
    SQLServerDataTable tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", java.sql.Types.TIME);
    tvp.addRow(TIME_TO_TEST);
    String sPrepStmt = "select * from dateandtime where my_time IN (select * from ?)";
    pstmt = connection.prepareStatement(sPrepStmt);
    ((SQLServerPreparedStatement) pstmt).setStructured(1, "timeTVP", tvp);
    rs = pstmt.executeQuery();
    rs.next();
    assertTrue(rs.getInt(1) == 42, "did not find correct timestamp");
    rs.close();
    pstmt.close();
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 40 with SQLServerPreparedStatement

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

the class SQLVariantResultSetTest method insertVarChar8001.

/**
 * Testing that inserting value more than 8000 on varchar(8000) should throw failure operand type clash
 *
 * @throws SQLException
 */
@Test
public void insertVarChar8001() throws SQLException {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 8001; i++) {
        buffer.append("a");
    }
    Utils.dropTableIfExists(tableName, stmt);
    stmt.executeUpdate("create table " + tableName + " (col1 sql_variant)");
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement("insert into " + tableName + " values (?)");
    pstmt.setObject(1, buffer.toString());
    try {
        pstmt.execute();
    } catch (SQLServerException e) {
        assertTrue(e.toString().contains("com.microsoft.sqlserver.jdbc.SQLServerException: Operand type clash"));
    }
}
Also used : SQLServerException(com.microsoft.sqlserver.jdbc.SQLServerException) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)43 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)31 Test (org.junit.jupiter.api.Test)31 ResultSet (java.sql.ResultSet)17 SQLServerDataTable (com.microsoft.sqlserver.jdbc.SQLServerDataTable)13 Connection (java.sql.Connection)9 SQLServerResultSet (com.microsoft.sqlserver.jdbc.SQLServerResultSet)8 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)6 Statement (java.sql.Statement)5 PreparedStatement (java.sql.PreparedStatement)4 Calendar (java.util.Calendar)4 SQLException (java.sql.SQLException)3 DisplayName (org.junit.jupiter.api.DisplayName)3 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)2 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)2 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)2 Properties (java.util.Properties)2 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)1 SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)1 SQLServerException (com.microsoft.sqlserver.jdbc.SQLServerException)1