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();
}
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);
}
}
}
}
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");
}
}
}
Aggregations