Search in sources :

Example 36 with SQLServerBulkCopy

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

the class BulkCopyWithSqlVariantTest method bulkCopyTestBinary20.

/**
 * test Binary value
 *
 * @throws SQLException
 */
@Test
public void bulkCopyTestBinary20() throws SQLException {
    String col1Value = "hello";
    beforeEachSetup("binary(20)", "'" + col1Value + "'");
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
    SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
    bulkCopy.setDestinationTableName(destTableName);
    bulkCopy.writeToServer(rs);
    bulkCopy.close();
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
    while (rs.next()) {
        assertTrue(Utils.parseByte(rs.getBytes(1), col1Value.getBytes()));
    }
}
Also used : SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 37 with SQLServerBulkCopy

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

the class BulkCopyWithSqlVariantTest method bulkCopyTestDate.

/**
 * test date value
 *
 * @throws SQLException
 */
@Test
public void bulkCopyTestDate() throws SQLException {
    String col1Value = "2015-05-05";
    beforeEachSetup("date", "'" + col1Value + "'");
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
    SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
    bulkCopy.setDestinationTableName(destTableName);
    bulkCopy.writeToServer(rs);
    bulkCopy.close();
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
    while (rs.next()) {
        assertEquals("" + rs.getDate(1), col1Value);
    }
}
Also used : SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 38 with SQLServerBulkCopy

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

the class BulkCopyResultSetCursorTest method testMultiplePreparedStatementAndResultSet.

/**
 * test with multiple prepared statements and result sets
 *
 * @throws SQLException
 */
@Test
public void testMultiplePreparedStatementAndResultSet() throws SQLException {
    try (Connection conn = DriverManager.getConnection(connectionString);
        Statement stmt = conn.createStatement()) {
        dropTables(stmt);
        createTables(stmt);
        populateSourceTable();
        try (ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery("select * from " + srcTable)) {
            try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
                bulkCopy.setDestinationTableName(desTable);
                bulkCopy.writeToServer(rs);
                verifyDestinationTableData(expectedBigDecimals.length);
            }
            rs.beforeFirst();
            try (SQLServerBulkCopy bulkCopy1 = new SQLServerBulkCopy(conn)) {
                bulkCopy1.setDestinationTableName(desTable);
                bulkCopy1.writeToServer(rs);
                verifyDestinationTableData(expectedBigDecimals.length * 2);
            }
            rs.beforeFirst();
            try (SQLServerBulkCopy bulkCopy2 = new SQLServerBulkCopy(conn)) {
                bulkCopy2.setDestinationTableName(desTable);
                bulkCopy2.writeToServer(rs);
                verifyDestinationTableData(expectedBigDecimals.length * 3);
            }
            String sql = "insert into " + desTable + " values (?,?,?,?)";
            Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            try (SQLServerPreparedStatement 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);
            }
            try (ResultSet rs2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery("select * from " + srcTable);
                SQLServerBulkCopy bulkCopy3 = new SQLServerBulkCopy(conn)) {
                bulkCopy3.setDestinationTableName(desTable);
                bulkCopy3.writeToServer(rs2);
                verifyDestinationTableData(expectedBigDecimals.length * 5);
            }
        }
    }
}
Also used : Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Calendar(java.util.Calendar) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 39 with SQLServerBulkCopy

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

the class BulkCopyResultSetCursorTest method testSelectMethodSetToCursor.

/**
 * Test a previous failure when setting SelectMethod to cursor and using the same connection to create Bulk Copy and result set.
 *
 * @throws SQLException
 */
@Test
public void testSelectMethodSetToCursor() throws SQLException {
    Properties info = new Properties();
    info.setProperty("SelectMethod", "cursor");
    try (Connection conn = DriverManager.getConnection(connectionString, info);
        Statement stmt = conn.createStatement()) {
        dropTables(stmt);
        createTables(stmt);
        populateSourceTable();
        try (ResultSet rs = conn.createStatement().executeQuery("select * from " + srcTable);
            SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
            bulkCopy.setDestinationTableName(desTable);
            bulkCopy.writeToServer(rs);
            verifyDestinationTableData(expectedBigDecimals.length);
        }
    }
}
Also used : Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 40 with SQLServerBulkCopy

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

the class BulkCopyResultSetCursorTest method serverCursorsTest.

private void serverCursorsTest(int resultSetType, int resultSetConcurrency) throws SQLException {
    try (Connection conn = DriverManager.getConnection(connectionString);
        Statement stmt = conn.createStatement()) {
        dropTables(stmt);
        createTables(stmt);
        populateSourceTable();
        try (ResultSet rs = conn.createStatement(resultSetType, resultSetConcurrency).executeQuery("select * from " + srcTable);
            SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
            bulkCopy.setDestinationTableName(desTable);
            bulkCopy.writeToServer(rs);
            verifyDestinationTableData(expectedBigDecimals.length);
        }
    }
}
Also used : Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)

Aggregations

SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)43 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)34 Test (org.junit.jupiter.api.Test)34 SQLException (java.sql.SQLException)8 ResultSet (java.sql.ResultSet)6 SQLServerException (com.microsoft.sqlserver.jdbc.SQLServerException)5 IOException (java.io.IOException)5 Connection (java.sql.Connection)5 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)4 Statement (java.sql.Statement)4 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)3 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)3 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)3 BigDecimal (java.math.BigDecimal)3 ColumnMap (com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap)1 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)1 DBTable (com.microsoft.sqlserver.testframework.DBTable)1 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1