Search in sources :

Example 1 with SQLServerStatement

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

the class DatabaseMetaDataForeignKeyTest method setupVariation.

@BeforeAll
private static void setupVariation() throws SQLException {
    conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
    SQLServerStatement stmt = (SQLServerStatement) conn.createStatement();
    catalog = conn.getCatalog();
    schema = conn.getSchema();
    connection.createStatement().executeUpdate("if object_id('" + table1 + "','U') is not null drop table " + table1);
    connection.createStatement().executeUpdate("if object_id('" + table2 + "','U') is not null drop table " + table2);
    stmt.execute("Create table " + table2 + " (c21 int NOT NULL PRIMARY KEY)");
    connection.createStatement().executeUpdate("if object_id('" + table3 + "','U') is not null drop table " + table3);
    stmt.execute("Create table " + table3 + " (c31 int NOT NULL PRIMARY KEY)");
    connection.createStatement().executeUpdate("if object_id('" + table4 + "','U') is not null drop table " + table4);
    stmt.execute("Create table " + table4 + " (c41 int NOT NULL PRIMARY KEY)");
    connection.createStatement().executeUpdate("if object_id('" + table5 + "','U') is not null drop table " + table5);
    stmt.execute("Create table " + table5 + " (c51 int NOT NULL PRIMARY KEY)");
    connection.createStatement().executeUpdate("if object_id('" + table1 + "','U') is not null drop table " + table1);
    stmt.execute("Create table " + table1 + " (c11 int primary key," + " c12 int FOREIGN KEY REFERENCES " + table2 + "(c21) ON DELETE no action ON UPDATE set default," + " c13 int FOREIGN KEY REFERENCES " + table3 + "(c31) ON DELETE cascade ON UPDATE set null," + " c14 int FOREIGN KEY REFERENCES " + table4 + "(c41) ON DELETE set null ON UPDATE cascade," + " c15 int FOREIGN KEY REFERENCES " + table5 + "(c51) ON DELETE set default ON UPDATE no action," + ")");
}
Also used : SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with SQLServerStatement

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

the class TVPIssuesTest method testExceptionWithInvalidStoredProcedureName.

/**
 * Test exception when invalid stored procedure name is used.
 *
 * @throws Exception
 */
@Test
public void testExceptionWithInvalidStoredProcedureName() throws Exception {
    SQLServerStatement st = (SQLServerStatement) connection.createStatement();
    ResultSet rs = st.executeQuery("select * from " + srcTable_varcharMax);
    dropProcedure();
    final String sql = "{call " + spName_varcharMax + "(?)}";
    SQLServerCallableStatement Cstmt = (SQLServerCallableStatement) connection.prepareCall(sql);
    try {
        Cstmt.setObject(1, rs);
        throw new Exception("Expected Exception for invalied stored procedure name is not thrown.");
    } catch (Exception e) {
        if (e instanceof SQLException) {
            assertTrue(e.getMessage().contains("Could not find stored procedure"), "Invalid Error Message.");
        } else {
            throw e;
        }
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) IOException(java.io.IOException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 3 with SQLServerStatement

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

the class TVPIssuesTest method tryTVPRSvarcharMax4000Issue.

@Test
public void tryTVPRSvarcharMax4000Issue() throws Exception {
    setup();
    SQLServerStatement st = (SQLServerStatement) connection.createStatement();
    ResultSet rs = st.executeQuery("select * from " + srcTable_varcharMax);
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + desTable_varcharMax + " select * from ? ;");
    pstmt.setStructured(1, tvp_varcharMax, rs);
    pstmt.execute();
    testCharDestTable();
}
Also used : ResultSet(java.sql.ResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 4 with SQLServerStatement

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

the class WrapperTest method unWrapFailureTest.

/**
 * Tests expected unwrapper failures
 * @throws Exception
 */
@Test
public void unWrapFailureTest() throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection con = DriverManager.getConnection(connectionString);
    SQLServerStatement stmt = (SQLServerStatement) con.createStatement();
    try {
        String str = "java.lang.String";
        boolean isWrapper = stmt.isWrapperFor(Class.forName(str));
        stmt.unwrap(Class.forName(str));
        assertEquals(isWrapper, false, "SQLServerStatement should not be a wrapper for string");
        stmt.unwrap(Class.forName(str));
        assertTrue(false, "An exception should have been thrown. This code should not be reached");
    } catch (SQLException ex) {
        Throwable t = ex.getCause();
        Class exceptionClass = Class.forName("java.lang.ClassCastException");
        assertEquals(t.getClass(), exceptionClass, "The cause in the exception class does not match");
    } catch (UnsupportedOperationException e) {
        assertEquals(System.getProperty("java.specification.version"), "1.5", "isWrapperFor should be supported in anything other than 1.5");
        assertEquals(e.getMessage(), "This operation is not supported.", "Wrong exception message");
    } finally {
        if (null != stmt) {
            stmt.close();
        }
        if (null != con) {
            con.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 5 with SQLServerStatement

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

the class BatchExecutionWithNullTest method terminateVariation.

@AfterAll
public static void terminateVariation() throws SQLException {
    connection = DriverManager.getConnection(connectionString);
    SQLServerStatement stmt = (SQLServerStatement) connection.createStatement();
    Utils.dropTableIfExists("esimple", stmt);
    if (null != pstmt) {
        pstmt.close();
    }
    if (null != pstmt1) {
        pstmt1.close();
    }
    if (null != stmt) {
        stmt.close();
    }
    if (null != rs) {
        rs.close();
    }
    if (null != connection) {
        connection.close();
    }
}
Also used : SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) AfterAll(org.junit.jupiter.api.AfterAll)

Aggregations

SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)11 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)5 Connection (java.sql.Connection)5 Test (org.junit.jupiter.api.Test)5 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Statement (java.sql.Statement)4 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)3 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 ISQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement)1 SQLServerColumnEncryptionJavaKeyStoreProvider (com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider)1 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)1 IOException (java.io.IOException)1