Search in sources :

Example 41 with SQLServerPreparedStatement

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

the class RegressionTest method testXmlQuery.

/**
 * Tests XML query
 *
 * @throws SQLException
 */
@Test
public void testXmlQuery() throws SQLException {
    assumeTrue("JDBC41".equals(Utils.getConfiguredProperty("JDBC_Version")), "Aborting test case as JDBC version is not compatible. ");
    Connection connection = DriverManager.getConnection(connectionString);
    Statement stmt = connection.createStatement();
    dropTables(stmt);
    createTable(stmt);
    String sql = "UPDATE " + xmlTableName + " SET [c2] = ?, [c3] = ?";
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(sql);
    pstmt.setObject(1, null);
    pstmt.setObject(2, null, Types.SQLXML);
    pstmt.executeUpdate();
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement(sql);
    pstmt.setObject(1, null, Types.SQLXML);
    pstmt.setObject(2, null);
    pstmt.executeUpdate();
    pstmt = (SQLServerPreparedStatement) connection.prepareStatement(sql);
    pstmt.setObject(1, null);
    pstmt.setObject(2, null, Types.SQLXML);
    pstmt.executeUpdate();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 42 with SQLServerPreparedStatement

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

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

the class PoolableTest method poolableTest.

/**
 * Poolable Test
 *
 * @throws SQLException
 * @throws ClassNotFoundException
 */
@Test
@DisplayName("Poolable Test")
public void poolableTest() throws SQLException, ClassNotFoundException {
    try (Connection conn = DriverManager.getConnection(connectionString);
        Statement statement = conn.createStatement()) {
        try {
            // First get the default values
            boolean isPoolable = ((SQLServerStatement) statement).isPoolable();
            assertEquals(isPoolable, false, "SQLServerStatement should not be Poolable by default");
            try (PreparedStatement prepStmt = connection.prepareStatement("select 1")) {
                isPoolable = ((SQLServerPreparedStatement) prepStmt).isPoolable();
                assertEquals(isPoolable, true, "SQLServerPreparedStatement should be Poolable by default");
            }
            try (CallableStatement callableStatement = connection.prepareCall("{  ? = CALL " + "ProcName" + " (?, ?, ?, ?) }")) {
                isPoolable = ((SQLServerCallableStatement) callableStatement).isPoolable();
                assertEquals(isPoolable, true, "SQLServerCallableStatement should be Poolable by default");
                // Now do couple of sets and gets
                ((SQLServerCallableStatement) callableStatement).setPoolable(false);
                assertEquals(((SQLServerCallableStatement) callableStatement).isPoolable(), false, "set did not work");
            }
            ((SQLServerStatement) statement).setPoolable(true);
            assertEquals(((SQLServerStatement) statement).isPoolable(), true, "set did not work");
        } catch (UnsupportedOperationException e) {
            assertEquals(System.getProperty("java.specification.version"), "1.5", "PoolableTest should be supported in anything other than 1.5");
            assertEquals(e.getMessage(), "This operation is not supported.", "Wrong exception message");
        }
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) PreparedStatement(java.sql.PreparedStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) DisplayName(org.junit.jupiter.api.DisplayName)

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