Search in sources :

Example 26 with SQLServerPreparedStatement

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

the class TVPResultSetCursorTest method testMultiplePreparedStatementAndResultSet.

/**
 * test with multiple prepared statements and result sets
 *
 * @throws SQLException
 */
@Test
public void testMultiplePreparedStatementAndResultSet() throws SQLException {
    conn = DriverManager.getConnection(connectionString);
    stmt = conn.createStatement();
    dropTVPS();
    dropTables();
    createTVPS();
    createTables();
    populateSourceTable();
    ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery("select * from " + srcTable);
    SQLServerPreparedStatement pstmt1 = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;");
    pstmt1.setStructured(1, tvpName, rs);
    pstmt1.execute();
    verifyDestinationTableData(expectedBigDecimals.length);
    rs.beforeFirst();
    pstmt1 = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;");
    pstmt1.setStructured(1, tvpName, rs);
    pstmt1.execute();
    verifyDestinationTableData(expectedBigDecimals.length * 2);
    rs.beforeFirst();
    SQLServerPreparedStatement pstmt2 = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;");
    pstmt2.setStructured(1, tvpName, rs);
    pstmt2.execute();
    verifyDestinationTableData(expectedBigDecimals.length * 3);
    String sql = "insert into " + desTable + " values (?,?,?,?)";
    Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    pstmt1 = (SQLServerPreparedStatement) conn.prepareStatement(sql);
    for (int i = 0; i < expectedBigDecimals.length; i++) {
        pstmt1.setBigDecimal(1, expectedBigDecimals[i]);
        pstmt1.setString(2, expectedStrings[i]);
        pstmt1.setTimestamp(3, expectedTimestamps[i], calGMT);
        pstmt1.setString(4, expectedStrings[i]);
        pstmt1.execute();
    }
    verifyDestinationTableData(expectedBigDecimals.length * 4);
    ResultSet rs2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery("select * from " + srcTable);
    pstmt1 = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;");
    pstmt1.setStructured(1, tvpName, rs2);
    pstmt1.execute();
    verifyDestinationTableData(expectedBigDecimals.length * 5);
    if (null != pstmt1) {
        pstmt1.close();
    }
    if (null != pstmt2) {
        pstmt2.close();
    }
    if (null != rs) {
        rs.close();
    }
    if (null != rs2) {
        rs2.close();
    }
}
Also used : Calendar(java.util.Calendar) ResultSet(java.sql.ResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 27 with SQLServerPreparedStatement

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

the class TVPResultSetCursorTest method testInvalidTVPName.

/**
 * Test exception when giving invalid TVP name
 *
 * @throws SQLException
 */
@Test
public void testInvalidTVPName() throws SQLException {
    Properties info = new Properties();
    info.setProperty("SelectMethod", "cursor");
    conn = DriverManager.getConnection(connectionString, info);
    stmt = conn.createStatement();
    dropTVPS();
    dropTables();
    createTVPS();
    createTables();
    populateSourceTable();
    ResultSet rs = conn.createStatement().executeQuery("select * from " + srcTable);
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;");
    pstmt.setStructured(1, "invalid" + tvpName, rs);
    try {
        pstmt.execute();
    } catch (SQLException e) {
        if (!e.getMessage().contains("Cannot find data type")) {
            throw e;
        }
    } finally {
        if (null != pstmt) {
            pstmt.close();
        }
        if (null != rs) {
            rs.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 28 with SQLServerPreparedStatement

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

the class TVPResultSetCursorTest method serverCursorsTest.

private void serverCursorsTest(int resultSetType, int resultSetConcurrency) throws SQLException {
    conn = DriverManager.getConnection(connectionString);
    stmt = conn.createStatement();
    dropTVPS();
    dropTables();
    createTVPS();
    createTables();
    populateSourceTable();
    ResultSet rs = conn.createStatement(resultSetType, resultSetConcurrency).executeQuery("select * from " + srcTable);
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement("INSERT INTO " + desTable + " select * from ? ;");
    pstmt.setStructured(1, tvpName, rs);
    pstmt.execute();
    verifyDestinationTableData(expectedBigDecimals.length);
    if (null != pstmt) {
        pstmt.close();
    }
    if (null != rs) {
        rs.close();
    }
}
Also used : ResultSet(java.sql.ResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)

Example 29 with SQLServerPreparedStatement

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

the class TVPResultSetCursorTest method populateSourceTable.

private static void populateSourceTable() throws SQLException {
    String sql = "insert into " + srcTable + " values (?,?,?,?)";
    Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement(sql);
    for (int i = 0; i < expectedBigDecimals.length; i++) {
        pstmt.setBigDecimal(1, expectedBigDecimals[i]);
        pstmt.setString(2, expectedStrings[i]);
        pstmt.setTimestamp(3, expectedTimestamps[i], calGMT);
        pstmt.setString(4, expectedStrings[i]);
        pstmt.execute();
    }
}
Also used : Calendar(java.util.Calendar) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)

Example 30 with SQLServerPreparedStatement

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

the class TVPTypesTest method testDateTime.

/**
 * Test a datetime support
 *
 * @throws SQLException
 */
@Test
public void testDateTime() throws SQLException {
    createTables("datetime");
    createTVPS("datetime");
    java.sql.Timestamp value = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.123");
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("c1", microsoft.sql.Types.DATETIME);
    tvp.addRow(value);
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + table + " select * from ? ;");
    pstmt.setStructured(1, tvpName, tvp);
    pstmt.execute();
    rs = conn.createStatement().executeQuery("select * from " + table);
    while (rs.next()) {
        assertEquals(((SQLServerResultSet) rs).getDateTime(1), value);
    }
    if (null != pstmt) {
        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)

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