2

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:

  • I'm programming under Windows using ADT
  • I'm runnig the application directly on my Galaxy S3 (Jelly Bean 4.1.2)
  • The application has just a button "Connect to the database"
  • 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:

  • Under package explorer, right click on app name
  • Build path\Add external archive..
  • Search manually for jtds-1.2.5.jar file, confirm

Then:

  • Copy manually the jtds-1.2.5.jar file
  • Under package explorer, right click on libs directory
  • Paste

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? :(

7
  • Is line 74 of MainActivity a line with: Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance(); ? If it is, then the problem is that your driver net.sourceforge.jtds.jdbc.Driver is not found. I.e. the problem is not in making a connection, but just with locating of the driver. Commented Jun 5, 2013 at 13:31
  • No, the line 74 is the exception management (in the catch statement, I forget to post it in the code). In the catch I just put: Toast.makeText(this,"Error: " + e.getMessage().toString(),Toast.LENGTH_LONG).show(); Commented Jun 5, 2013 at 13:47
  • That's it, if I take off the error management in the "catch" the app doesn't crash. But I still have no SQL connection :( Commented Jun 5, 2013 at 13:49
  • No connection means DbConn is null, right? When I was struggling to make work FireBird android driver, the driver always returned some sane exception message (e.g. 'Server not found'). I suspect the error handling - It seems that the null exception is thrown at line 74. Maybe the e.getMessage() is null so calling .toString() fails. It's safer to use just: "Error: " + e.getMessage() - it won't crash on null. Commented Jun 5, 2013 at 13:54
  • It's exaclty like that. I took off the ".toString()" and now I've no crash, just "Error: null" as you said. I still don't understand why the connectionstring and driver settings are not working Commented Jun 5, 2013 at 13:58

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Take a look at this: stackoverflow.com/questions/17826096/… but again, i don't think expose your database directly is a good practice though.
1

Try: before of DriverManager.getConnection

StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Comments

0
public void ConnectToDatabase(){
    try {

         // SET CONNECTIONSTRING
         Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
         String username = "XXXXXXXXX";
            String password = "XXXXXX";
         Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.188.4.83:1433/DATABASE;user=" + username + ";password=" + password);

         Log.w("Connection","open");
        Statement stmt = DbConn.createStatement();
        ResultSet reset = stmt.executeQuery(" select * from users ");


         EditText num = (EditText) findViewById(R.id.displaymessage);
        num.setText(reset.getString(1));

        DbConn.close();

        } catch (Exception e)
        {
        Log.w("Error connection","" + e.getMessage());
        }
}

Comments

0

On the computer that has the sql also check if all the types of connections are enabled, not only named pipes. Your first method should work. If not try to improve your code from more sugestions. That part of the connection took me an entire week. The default for sql server express is to use named pipes only. On your connection string you shuld also add

    Network Library=DBMSSOCN;

after password. This will tell the sql server to use TCP and not named pipes. Probably your first code was fine. I tried a lot of things and a lot of code, and i only needed to add that litle thing. If you do not specify the connection mode it fails for remote connections. Only local connections work.

Comments

0

Bump any updates on this OP?

p.s: i just saw that there was a missing ";" semi-colon after the server name.

Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://12.34.56.78:1433/ DatabaseName;user=myusername;password=mypassword");

Could that have been the cause for error?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.