title: Connect android app to sql server database
body: <p>I'm pretty new on java and android programming.
  I'm trying to connect an android application to a SQL Server database installed on a Windows laptop.</p>
  
  <p>I searched in the all network and I fonud a lot of solutions, without success.
  My situation is the following:</p>
  
  <ul>
  <li>I'm programming under Windows using ADT</li>
  <li>I'm runnig the application directly on my Galaxy S3 (Jelly Bean 4.1.2)</li>
  <li>The application has just a button "Connect to the database"</li>
  <li><p>The code is the following (placed in the MainActivity.java):</p>
  
  <pre><code>public void ConnectToDatabase(View View){
      try {
           // SET CONNECTIONSTRING
           Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
           Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://12.34.56.78:1433/DatabaseName;user=myusername;password=mypassword");
  
           // CONNECTION OK: SHOW MESSAGE
           Toast.makeText(this, "Connection successful!", Toast.LENGTH_LONG).show();
       }
  }
  </code></pre></li>
  </ul>
  
  <p>I added the <strong>jtds-1.2.5.jar</strong> twice in those ways:</p>
  
  <ul>
  <li>Under package explorer, right click on app name</li>
  <li>Build path\Add external archive..</li>
  <li>Search manually for jtds-1.2.5.jar file, confirm</li>
  </ul>
  
  <p>Then:</p>
  
  <ul>
  <li>Copy manually the jtds-1.2.5.jar file</li>
  <li>Under package explorer, right click on libs directory</li>
  <li>Paste</li>
  </ul>
  
  <p>On my laptop the SQL Browser service is enabled, the port 1433 is correctly open and the windows firewall service is disabled. I can connect normally to my database from another PC.</p>
  
  <p>I'm sharing the internet connection of my notebook using Connectify with my android device. From the laptop I can ping the smartphone with success. I also tried this configuration using another laptop without problems.</p>
  
  <p>When I press the "connect to database" button I receive the following error message:</p>
  
  <pre><code>Intent error: Network error IOException: socked failed: EACCES (Permission denied)
  </code></pre>
  
  <p>I searched online and I found this solution. Put the internet permission in the Manifest file:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <p>After that, when I press the "connect to database" button the app just crashes. That's the output of the LogCat:</p>
  
  <pre><code>D/dalvikvm(24311): GC_CONCURRENT freed 180K, 6% free 12426K/13127K, paused 12ms+2ms, total 39ms
  D/AndroidRuntime(24311): Shutting down VM
  W/dalvikvm(24311): threadid=1: thread exiting with uncaught exception (group=0x40f382a0)
  E/AndroidRuntime(24311): FATAL EXCEPTION: main
  E/AndroidRuntime(24311): java.lang.IllegalStateException: Could not execute method of the activity
  E/AndroidRuntime(24311):    at android.view.View$1.onClick(View.java:3691)
  E/AndroidRuntime(24311):    at android.view.View.performClick(View.java:4211)
  E/AndroidRuntime(24311):    at android.view.View$PerformClick.run(View.java:17267)
  E/AndroidRuntime(24311):    at android.os.Handler.handleCallback(Handler.java:615)
  E/AndroidRuntime(24311):    at android.os.Handler.dispatchMessage(Handler.java:92)
  E/AndroidRuntime(24311):    at android.os.Looper.loop(Looper.java:137)
  E/AndroidRuntime(24311):    at android.app.ActivityThread.main(ActivityThread.java:4898)
  E/AndroidRuntime(24311):    at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(24311):    at java.lang.reflect.Method.invoke(Method.java:511)
  E/AndroidRuntime(24311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
  E/AndroidRuntime(24311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
  E/AndroidRuntime(24311):    at dalvik.system.NativeStart.main(Native Method)
  E/AndroidRuntime(24311): Caused by: java.lang.reflect.InvocationTargetException
  E/AndroidRuntime(24311):    at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(24311):    at java.lang.reflect.Method.invoke(Method.java:511)
  E/AndroidRuntime(24311):    at android.view.View$1.onClick(View.java:3686)
  E/AndroidRuntime(24311):    ... 11 more
  E/AndroidRuntime(24311): Caused by: java.lang.NullPointerException
  E/AndroidRuntime(24311):    at com.example.app1.MainActivity.ConnectToDatabase(MainActivity.java:74)
  E/AndroidRuntime(24311):    ... 14 more
  </code></pre>
  
  <p>Can someone help me with this?</p>
  
  <p>Thanks in advance</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>I think that the JDBC driver is loaded correctly by the app. The application throw the exception when it tries to connect to the SQL database. As long as I'm tring to connect to a \SQLEXPRESS database, I changed the connectionstring as follow:</p>
  
  <pre><code>Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://12.34.56.78:1433/DatabaseName;user=myusername;password=mypassword;instance=SQLEXPRESS")
  </code></pre>
  
  <p>If I leave the "istance=SQLEXPRESS" the app throw the following <strong>SQLException</strong>:</p>
  
  <pre><code>"Unable to get information from SQL Server: 12.34.56.78."
  </code></pre>
  
  <p>If I take off the istance from the connectionstring the errori I receive is the following:</p>
  
  <pre><code>Error: null
  </code></pre>
  
  <p>Any Ideas? :(</p>