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.
I searched in the all network and I fonud a lot of solutions, without success. My situation is the following:
The code is the following (placed in the MainActivity.java):
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();
}
}
I added the jtds-1.2.5.jar twice in those ways:
Then:
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.
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.
When I press the "connect to database" button I receive the following error message:
Intent error: Network error IOException: socked failed: EACCES (Permission denied)
I searched online and I found this solution. Put the internet permission in the Manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
After that, when I press the "connect to database" button the app just crashes. That's the output of the LogCat:
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
Can someone help me with this?
Thanks in advance
EDIT
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:
Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://12.34.56.78:1433/DatabaseName;user=myusername;password=mypassword;instance=SQLEXPRESS")
If I leave the "istance=SQLEXPRESS" the app throw the following SQLException:
"Unable to get information from SQL Server: 12.34.56.78."
If I take off the istance from the connectionstring the errori I receive is the following:
Error: null
Any Ideas? :(
This may not be relevant (since i never tried/heard to access a sql database directly from android app) but why don't you create a web service for database? Since your database sits on your host not on your phone memory card, eventually you will need to create a web service so that your app can access the database.