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