use of com.microsoft.sqlserver.jdbc.SQLServerDataSource in project mssql-jdbc by Microsoft.
the class executeStoredProcedure method main.
public static void main(String[] args) {
// Declare the JDBC objects.
Connection con = null;
Statement stmt = 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();
createTable(con);
createStoredProcedure(con);
// Create test data as an example.
StringBuffer buffer = new StringBuffer(4000);
for (int i = 0; i < 4000; i++) buffer.append((char) ('A'));
PreparedStatement pstmt = con.prepareStatement("UPDATE Document_JDBC_Sample " + "SET DocumentSummary = ? WHERE (DocumentID = 1)");
pstmt.setString(1, buffer.toString());
pstmt.executeUpdate();
pstmt.close();
// Query test data by using a stored procedure.
CallableStatement cstmt = con.prepareCall("{call GetLargeDataValue(?, ?, ?, ?)}");
cstmt.setInt(1, 1);
cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
cstmt.registerOutParameter(3, java.sql.Types.CHAR);
cstmt.registerOutParameter(4, java.sql.Types.LONGVARCHAR);
// Display the response buffering mode.
SQLServerCallableStatement SQLcstmt = (SQLServerCallableStatement) cstmt;
System.out.println("Response buffering mode is: " + SQLcstmt.getResponseBuffering());
SQLcstmt.execute();
System.out.println("DocumentID: " + cstmt.getInt(2));
System.out.println("Document_Title: " + cstmt.getString(3));
Reader reader = SQLcstmt.getCharacterStream(4);
// If your application needs to re-read any portion of the value,
// it must call the mark method on the InputStream or Reader to
// start buffering data that is to be re-read after a subsequent
// call to the reset method.
reader.mark(4000);
// Read the first half of data.
char[] output1 = new char[2000];
reader.read(output1);
String stringOutput1 = new String(output1);
// Reset the stream.
reader.reset();
// Read all the data.
char[] output2 = new char[4000];
reader.read(output2);
String stringOutput2 = new String(output2);
System.out.println("Document_Summary in half: " + stringOutput1);
System.out.println("Document_Summary: " + stringOutput2);
// Close the stream.
reader.close();
}// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.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 ConnectionDriverTest method testIncorrectPassword.
@Test
public void testIncorrectPassword() throws SQLException {
long timerStart = 0;
long timerEnd = 0;
Connection con = null;
final long milsecs = threshHoldForNoRetryInMilliseconds;
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
ds.setPassword(RandomUtil.getIdentifier("Password"));
timerStart = System.currentTimeMillis();
con = ds.getConnection();
} catch (Exception e) {
assertTrue(e.getMessage().contains("Login failed"));
timerEnd = System.currentTimeMillis();
}
long timeDiff = timerEnd - timerStart;
assertTrue(con == null, "Should not have connected.");
assertTrue(timeDiff <= milsecs, "Exited in more than " + (milsecs / 1000) + " seconds.");
}
use of com.microsoft.sqlserver.jdbc.SQLServerDataSource in project mssql-jdbc by Microsoft.
the class ConnectionDriverTest method testThreadInterruptedStatus.
/**
* Test thread's interrupt status is not cleared.
*
* @throws InterruptedException
*/
@Test
@Tag("slow")
public void testThreadInterruptedStatus() throws InterruptedException {
Runnable runnable = new Runnable() {
public void run() {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setServerName("invalidServerName" + UUID.randomUUID());
ds.setLoginTimeout(5);
try {
ds.getConnection();
} catch (SQLException e) {
isInterrupted = Thread.currentThread().isInterrupted();
}
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<?> future = executor.submit(runnable);
Thread.sleep(1000);
// interrupt the thread in the Runnable
future.cancel(true);
Thread.sleep(8000);
executor.shutdownNow();
assertTrue(isInterrupted, "Thread's interrupt status is not set.");
}
use of com.microsoft.sqlserver.jdbc.SQLServerDataSource in project mssql-jdbc by Microsoft.
the class ConnectionDriverTest method testConnectionClosed.
@Test
public void testConnectionClosed() throws SQLException {
assumeTrue(!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString)), "Skipping test case on Azure SQL.");
SQLServerDataSource mds = new SQLServerDataSource();
mds.setURL(connectionString);
Connection con = mds.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
boolean exceptionThrown = false;
try {
stmt.executeUpdate("RAISERROR ('foo', 20,1) WITH LOG");
} catch (Exception e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown, "Expected exception is not thrown.");
// check to make sure that connection is closed.
assertTrue(con.isClosed(), "Connection is not closed.");
}
use of com.microsoft.sqlserver.jdbc.SQLServerDataSource in project mssql-jdbc by Microsoft.
the class ConnectionDriverTest method testDataSource.
/**
* test SSL properties with SQLServerDataSource
*/
@Test
public void testDataSource() {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("User");
ds.setPassword("sUser");
ds.setApplicationName("User");
ds.setURL("jdbc:sqlserver://" + randomServer + ";packetSize=512");
String trustStore = "Store";
String trustStorePassword = "pwd";
ds.setTrustStore(trustStore);
ds.setEncrypt(true);
ds.setTrustStorePassword(trustStorePassword);
ds.setTrustServerCertificate(true);
assertEquals(trustStore, ds.getTrustStore(), "Values are different");
assertEquals(true, ds.getEncrypt(), "Values are different");
assertEquals(true, ds.getTrustServerCertificate(), "Values are different");
}
Aggregations