title: SSL Connection to mssql from a Java Program
body: <p>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:</p>
  
  <pre><code>String dbUrl = "jdbc:sqlserver://" + server + ":" + port
          + ";databaseName=" + database + ";user="+   username
                      + ";password=" + password
                      + ";encrypt="+true
                      + ";integratedSecurity="+true
                      + ";trustServerCertificate="+false
                      + ";hostNameInCertificate=?"
                      + ";trustStore=?;trustStorePassword=?";
  </code></pre>
  
  <p>I am now confused what should be placed at "?" position in the connection string above. </p>
  
  <p>Also I am able to connect with mssql from management studio by following approach:</p>
  
  <ul>
  <li>To Create a Self Signed Certificate:</li>
  <li>Go to Control Panel</li>
  <li>Administrative tools</li>
  <li>IIS Manager</li>
  <li>Server Certificates</li>
  <li><p>Then on server certificate, on right Panel, Click Create self-signed certificate and Give Friendly Name. > OK. Now certificate is ready.</p></li>
  <li><p>Open MMC, by typing mmc in cmd</p></li>
  <li>Add/Remove Snap in</li>
  <li>Choose Certificate</li>
  <li>Add</li>
  <li>Next, Choose Local Computer, Finish,OK</li>
  <li>Expand Certificates, Personal, Certificates, we can see recently added certificate.</li>
  <li>Select certificate, Right Click,  All Tasks, Manage Private Keys</li>
  <li><p>Add MSSQL User, add give read permission.</p></li>
  <li><p>Now open SQL Server Configuration Manager</p></li>
  <li>SQL server network configuration, properties</li>
  <li>Set Force Encryption to Yes, </li>
  <li><p>In certificate Tab, Choose the Certificate recently added</p></li>
  <li><p>Restart SQL Server Services.</p></li>
  <li>Try encryption connection to database and connect.</li>
  </ul>
  
  <p>But how to connect it with java program? What should be kept in trustStore and trustStorePassword fields?</p>
  
  <p>Working Environment: Windows 8, and mssql server 2012</p>