title: Sql Database connect Using the JDBC Driver with android
body: <p>I'm trying to connect to SQL Serverusing JDBC in an android app. I imported sqljdbc4.jar into my application but when i get to </p>
  
  <blockquote>
    <p>conn = DriverManager.getConnection(connString);</p>
  </blockquote>
  
  <p>i get the error below: the connection string is </p>
  
  <blockquote>
    <p>jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;encrypt=fasle;user=username;password=password;</p>
  </blockquote>
  
  <p>Can any one tell me what is going wrong? 
   If i put the same code into a regular java application it runs fine.</p>
  
  <p><strong>Basic Java</strong></p>
  
  <pre><code>public class Test {
      public void dbConnect(String db_connect_string, String db_userid,
              String db_password) {
          try {
  
              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              Connection conn = DriverManager.getConnection(db_connect_string,
                      db_userid, db_password);
              System.out.println("connected");
              Statement statement = conn.createStatement();
              String queryString = "select * from sysobjects where type='u'";
              ResultSet rs = statement.executeQuery(queryString);
              while (rs.next()) {
                  System.out.println(rs.getString(1));
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          Test connServer = new Test();
          connServer.dbConnect("jdbc:sqlserver://Serverlocalip:1433", "sa",
                  "sqlserver");
      }
  }
  </code></pre>
  
  <p><strong>Android Code:</strong></p>
  
  <pre><code>public List&lt;String&gt; dbConnect(String Host, String Port, String db_userid,
              String db_password) {
          List&lt;String&gt; Db_list = new ArrayList&lt;String&gt;();
          try {
              // String ConnectionString = "jdbc:sqlserver://" + Host + ":" +
              // Port;
              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              Connection conn = DriverManager.getConnection(
                      "jdbc:sqlserver://Serverlocalip:1433", db_userid,
                      db_password);
              System.out.println("connected");
              Statement statement = conn.createStatement();
              String queryString = "select * from sysobjects where type='u'";
              ResultSet rs = statement.executeQuery(queryString);
              while (rs.next()) {
                  System.out.println(rs.getString(1));
                  Db_list.add(rs.getString(1));
              }
          } catch (Exception e) {
              Db_list.add("Error");
              e.printStackTrace();
          }
          return Db_list;
      }
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>07-24 17:25:02.058: W/System.err(22227): com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed".
  07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1352)
  07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1466)
  07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1042)
  07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
  07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
  07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
  07-24 17:25:02.228: W/System.err(22227):    at java.sql.DriverManager.getConnection(DriverManager.java:175)
  07-24 17:25:02.238: W/System.err(22227):    at java.sql.DriverManager.getConnection(DriverManager.java:209)
  07-24 17:25:02.238: W/System.err(22227):    at com.example.sqlserverconnector.MainActivity.dbConnect(MainActivity.java:87)
  07-24 17:25:02.238: W/System.err(22227):    at com.example.sqlserverconnector.MainActivity$1.onClick(MainActivity.java:39)
  07-24 17:25:02.238: W/System.err(22227):    at android.view.View.performClick(View.java:3574)
  07-24 17:25:02.238: W/System.err(22227):    at android.view.View$PerformClick.run(View.java:14293)
  07-24 17:25:02.238: W/System.err(22227):    at android.os.Handler.handleCallback(Handler.java:605)
  07-24 17:25:02.248: W/System.err(22227):    at android.os.Handler.dispatchMessage(Handler.java:92)
  07-24 17:25:02.248: W/System.err(22227):    at android.os.Looper.loop(Looper.java:137)
  07-24 17:25:02.248: W/System.err(22227):    at android.app.ActivityThread.main(ActivityThread.java:4448)
  07-24 17:25:02.248: W/System.err(22227):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-24 17:25:02.248: W/System.err(22227):    at java.lang.reflect.Method.invoke(Method.java:511)
  07-24 17:25:02.248: W/System.err(22227):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
  07-24 17:25:02.248: W/System.err(22227):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
  07-24 17:25:02.258: W/System.err(22227):    at dalvik.system.NativeStart.main(Native Method)
  07-24 17:25:02.258: W/System.err(22227): Caused by: java.net.SocketException: Socket closed
  07-24 17:25:02.318: W/System.err(22227):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
  07-24 17:25:02.318: W/System.err(22227):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
  07-24 17:25:02.318: W/System.err(22227):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
  07-24 17:25:02.318: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1433)
  </code></pre>
  
  <p>any help would be great and Thanks in advance</p>