SSL Connection to mssql from a Java Program

Viewed 28022

How can I achieve SSL connection to mssql server through a java program for testing purpose using self-signed certificate? Currently I have tried connection using connection string as follows:

String dbUrl = "jdbc:sqlserver://" + server + ":" + port
        + ";databaseName=" + database + ";user="+   username
                    + ";password=" + password
                    + ";encrypt="+true
                    + ";integratedSecurity="+true
                    + ";trustServerCertificate="+false
                    + ";hostNameInCertificate=?"
                    + ";trustStore=?;trustStorePassword=?";

I am now confused what should be placed at "?" position in the connection string above.

Also I am able to connect with mssql from management studio by following approach:

  • To Create a Self Signed Certificate:
  • Go to Control Panel
  • Administrative tools
  • IIS Manager
  • Server Certificates
  • Then on server certificate, on right Panel, Click Create self-signed certificate and Give Friendly Name. > OK. Now certificate is ready.

  • Open MMC, by typing mmc in cmd

  • Add/Remove Snap in
  • Choose Certificate
  • Add
  • Next, Choose Local Computer, Finish,OK
  • Expand Certificates, Personal, Certificates, we can see recently added certificate.
  • Select certificate, Right Click, All Tasks, Manage Private Keys
  • Add MSSQL User, add give read permission.

  • Now open SQL Server Configuration Manager

  • SQL server network configuration, properties
  • Set Force Encryption to Yes,
  • In certificate Tab, Choose the Certificate recently added

  • Restart SQL Server Services.

  • Try encryption connection to database and connect.

But how to connect it with java program? What should be kept in trustStore and trustStorePassword fields?

Working Environment: Windows 8, and mssql server 2012

1 Answers
Related