Search in sources :

Example 16 with SQLServerPreparedStatement

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

the class RegressionTest method testUpdateQuery.

/**
 * Tests update query
 *
 * @throws SQLException
 */
@Test
public void testUpdateQuery() throws SQLException {
    assumeTrue("JDBC41".equals(Utils.getConfiguredProperty("JDBC_Version")), "Aborting test case as JDBC version is not compatible. ");
    SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
    String sql;
    SQLServerPreparedStatement pstmt = null;
    JDBCType[] targets = { JDBCType.INTEGER, JDBCType.SMALLINT };
    int rows = 3;
    final String tableName = "[updateQuery]";
    Statement stmt = con.createStatement();
    Utils.dropTableIfExists(tableName, stmt);
    stmt.executeUpdate("CREATE TABLE " + tableName + " (" + "c1 int null," + "PK int NOT NULL PRIMARY KEY" + ")");
    /*
         * populate table
         */
    sql = "insert into " + tableName + " values(" + "?,?" + ")";
    pstmt = (SQLServerPreparedStatement) con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, connection.getHoldability());
    for (int i = 1; i <= rows; i++) {
        pstmt.setObject(1, i, JDBCType.INTEGER);
        pstmt.setObject(2, i, JDBCType.INTEGER);
        pstmt.executeUpdate();
    }
    /*
         * Update table
         */
    sql = "update " + tableName + " SET c1= ? where PK =1";
    for (int i = 1; i <= rows; i++) {
        pstmt = (SQLServerPreparedStatement) con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        for (JDBCType target : targets) {
            pstmt.setObject(1, 5 + i, target);
            pstmt.executeUpdate();
        }
    }
    /*
         * Verify
         */
    ResultSet rs = stmt.executeQuery("select * from " + tableName);
    rs.next();
    assertEquals(rs.getInt(1), 8, "Value mismatch");
    if (null != stmt)
        stmt.close();
    if (null != con)
        con.close();
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) JDBCType(java.sql.JDBCType) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 17 with SQLServerPreparedStatement

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

the class Wrapper42Test method PreparedSatement42Test.

/**
 * Tests creation of SQLServerPreparedSatement42 object
 *
 * @throws SQLException
 */
@Test
public void PreparedSatement42Test() throws SQLException {
    String sql = "SELECT SUSER_SNAME()";
    PreparedStatement pstmt = connection.prepareStatement(sql);
    if (1.8d <= javaVersion && 4 == major && 2 == minor) {
        assertTrue(pstmt instanceof SQLServerPreparedStatement42);
    } else {
        assertTrue(pstmt instanceof SQLServerPreparedStatement);
    }
}
Also used : SQLServerPreparedStatement42(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement42) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) PreparedStatement(java.sql.PreparedStatement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 18 with SQLServerPreparedStatement

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

the class JDBCEncryptionDecryptionTest method testDate.

private void testDate(SQLServerStatement stmt, LinkedList<Object> values1) throws SQLException {
    String sql = "select * from " + dateTable;
    try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql, stmtColEncSetting)) {
        try (ResultSet rs = (stmt == null) ? pstmt.executeQuery() : stmt.executeQuery(sql)) {
            int numberOfColumns = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                // testGetStringForDate(rs, numberOfColumns, values1); //TODO: Disabling, since getString throws verification error for zero temporal
                // types
                testGetObjectForTemporal(rs, numberOfColumns, values1);
                testGetDate(rs, numberOfColumns, values1);
            }
        }
    }
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerResultSet(com.microsoft.sqlserver.jdbc.SQLServerResultSet) ResultSet(java.sql.ResultSet)

Example 19 with SQLServerPreparedStatement

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

the class JDBCEncryptionDecryptionTest method testChar.

private void testChar(SQLServerStatement stmt, String[] values) throws SQLException {
    String sql = "select * from " + charTable;
    try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql, stmtColEncSetting)) {
        try (ResultSet rs = (stmt == null) ? pstmt.executeQuery() : stmt.executeQuery(sql)) {
            int numberOfColumns = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                testGetString(rs, numberOfColumns, values);
                testGetObject(rs, numberOfColumns, values);
            }
        }
    }
}
Also used : SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerResultSet(com.microsoft.sqlserver.jdbc.SQLServerResultSet) ResultSet(java.sql.ResultSet)

Example 20 with SQLServerPreparedStatement

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

the class JDBCEncryptionDecryptionTest method testNumeric.

private void testNumeric(Statement stmt, String[] numericValues, boolean isNull) throws SQLException {
    String sql = "select * from " + numericTable;
    try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql, stmtColEncSetting)) {
        try (SQLServerResultSet rs = (stmt == null) ? (SQLServerResultSet) pstmt.executeQuery() : (SQLServerResultSet) stmt.executeQuery(sql)) {
            int numberOfColumns = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                testGetString(rs, numberOfColumns, numericValues);
                testGetObject(rs, numberOfColumns, numericValues);
                testGetBigDecimal(rs, numberOfColumns, numericValues);
                if (!isNull)
                    testWithSpecifiedtype(rs, numberOfColumns, numericValues);
                else {
                    String[] nullNumericValues = { "false", "0", "0", "0", "0", "0.0", "0.0", "0.0", null, null, null, null, null, null, null, null };
                    testWithSpecifiedtype(rs, numberOfColumns, nullNumericValues);
                }
            }
        }
    }
}
Also used : SQLServerResultSet(com.microsoft.sqlserver.jdbc.SQLServerResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)

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