Search in sources :

Example 6 with SQLServerDataSource

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

the class DBMetadataTest method testDatabaseMetaData.

@Test
public void testDatabaseMetaData() throws SQLException {
    String functionName = RandomUtil.getIdentifier("proc");
    functionName = DBTable.escapeIdentifier(functionName);
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setURL(connectionString);
    String sqlDropFunction = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo]." + functionName + "')" + "and xtype in (N'FN', N'IF', N'TF'))" + "drop function " + functionName;
    String sqlCreateFunction = "CREATE  FUNCTION " + functionName + " (@text varchar(8000), @delimiter varchar(20) = ' ') RETURNS @Strings TABLE " + "(position int IDENTITY PRIMARY KEY, value varchar(8000)) AS BEGIN INSERT INTO @Strings VALUES ('DDD') RETURN END ";
    try (Connection con = ds.getConnection();
        Statement stmt = con.createStatement()) {
        // drop function
        stmt.execute(sqlDropFunction);
        // create function
        stmt.execute(sqlCreateFunction);
        DatabaseMetaData md = con.getMetaData();
        try (ResultSet arguments = md.getProcedureColumns(null, null, null, "@TABLE_RETURN_VALUE")) {
            if (arguments.next()) {
                arguments.getString("COLUMN_NAME");
                // call this function to make sure it does not crash
                arguments.getString("DATA_TYPE");
            }
        }
        stmt.execute(sqlDropFunction);
    }
}
Also used : SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 7 with SQLServerDataSource

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

the class NativeMSSQLDataSourceTest method testSerial.

private SQLServerDataSource testSerial(SQLServerDataSource ds) throws IOException, ClassNotFoundException {
    try (java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
        java.io.ObjectOutput objectOutput = new java.io.ObjectOutputStream(outputStream)) {
        objectOutput.writeObject(ds);
        objectOutput.flush();
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
            SQLServerDataSource dtn;
            dtn = (SQLServerDataSource) in.readObject();
            return dtn;
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with SQLServerDataSource

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

the class NativeMSSQLDataSourceTest method testDSTSPassword.

@Test
public void testDSTSPassword() throws ClassNotFoundException, IOException, SQLException {
    SQLServerDataSource ds = new SQLServerDataSource();
    System.setProperty("java.net.preferIPv6Addresses", "true");
    ds.setURL(connectionString);
    ds.setTrustStorePassword("wrong_password");
    try (Connection conn = ds.getConnection()) {
    }
    ds = testSerial(ds);
    try (Connection conn = ds.getConnection()) {
    } catch (SQLException e) {
        assertEquals("The DataSource trustStore password needs to be set.", e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 9 with SQLServerDataSource

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

the class connectDS method main.

public static void main(String[] args) {
    // Declare the JDBC objects.
    Connection con = null;
    Statement stmt = null;
    CallableStatement cstmt = null;
    ResultSet rs = null;
    String serverName = null;
    String portNumber = null;
    String databaseName = null;
    String username = null;
    String password = null;
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
        System.out.print("Enter server name: ");
        serverName = br.readLine();
        System.out.print("Enter port number: ");
        portNumber = br.readLine();
        System.out.print("Enter database name: ");
        databaseName = br.readLine();
        System.out.print("Enter username: ");
        username = br.readLine();
        System.out.print("Enter password: ");
        password = br.readLine();
        // Establish the connection.
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName(serverName);
        ds.setPortNumber(Integer.parseInt(portNumber));
        ds.setDatabaseName(databaseName);
        ds.setUser(username);
        ds.setPassword(password);
        con = ds.getConnection();
        System.out.println();
        System.out.println("Connection established successfully.");
        // Create and execute an SQL statement that returns user name.
        String SQL = "SELECT SUSER_SNAME()";
        stmt = con.createStatement();
        rs = stmt.executeQuery(SQL);
        // Iterate through the data in the result set and display it.
        while (rs.next()) {
            System.out.println("user name: " + rs.getString(1));
        }
    }// Handle any errors that may have occurred.
     catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (Exception e) {
            }
        if (cstmt != null)
            try {
                cstmt.close();
            } catch (Exception e) {
            }
        if (con != null)
            try {
                con.close();
            } catch (Exception e) {
            }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) CallableStatement(java.sql.CallableStatement) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) BufferedReader(java.io.BufferedReader)

Example 10 with SQLServerDataSource

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

the class FipsTest method fipsDataSourcePropertyTest.

/**
 * Tests after removing all FIPS related properties.
 *
 * @throws Exception
 */
@Test
public void fipsDataSourcePropertyTest() throws Exception {
    SQLServerDataSource ds = new SQLServerDataSource();
    setDataSourceProperties(ds);
    ds.setFIPS(false);
    ds.setEncrypt(false);
    ds.setTrustStoreType("JKS");
    Connection con = ds.getConnection();
    Assertions.assertTrue(!StringUtils.isEmpty(con.getSchema()));
    con.close();
    con = null;
}
Also used : SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test)

Aggregations

SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)26 Test (org.junit.jupiter.api.Test)22 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)19 Connection (java.sql.Connection)19 SQLException (java.sql.SQLException)14 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)9 ISQLServerConnection (com.microsoft.sqlserver.jdbc.ISQLServerConnection)7 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)7 Statement (java.sql.Statement)7 PooledConnection (javax.sql.PooledConnection)7 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)6 ISQLServerDataSource (com.microsoft.sqlserver.jdbc.ISQLServerDataSource)5 ResultSet (java.sql.ResultSet)5 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 CallableStatement (java.sql.CallableStatement)3 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2