creating a connection between MS SQL database and Android

trying to create a connection between MS SQL database and Android but emullator showing messAGE 'Unfortunately your app has stopped'.

this is the Logcat:
08-10 15:34:50.000  19410-19410/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
08-10 15:34:50.700  19410-19422/com.dk.dkmobile I/art﹕ Background sticky concurrent mark sweep GC freed 6621(494KB) AllocSpace objects, 6(210KB) LOS objects, 0% free, 1917KB/1917KB, paused 56.105ms total 184.819ms
08-10 15:34:50.722  19410-19422/com.dk.dkmobile W/art﹕ Suspending all threads took: 21.392ms
08-10 15:34:50.853  19410-19422/com.dk.dkmobile W/art﹕ Suspending all threads took: 32.373ms
08-10 15:34:50.969  19410-19410/com.dk.dkmobile E/Error﹕ Single-Sign-On is only supported on Windows. Please specify a user name.
08-10 15:34:50.996  19410-19438/com.dk.dkmobile D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-10 15:34:51.009  19410-19410/com.dk.dkmobile D/﹕ HostConnection::get() New Host Connection established 0xb3ff01b0, tid 19410
08-10 15:34:52.132  19410-19410/com.dk.dkmobile D/Atlas﹕ Validating map...
08-10 15:34:52.229  19410-19438/com.dk.dkmobile D/﹕ HostConnection::get() New Host Connection established 0xb3ff04f0, tid 19438
08-10 15:34:52.279  19410-19438/com.dk.dkmobile I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-10 15:34:52.393  19410-19438/com.dk.dkmobile D/OpenGLRenderer﹕ Enabling debug mode 0
08-10 15:34:52.474  19410-19438/com.dk.dkmobile W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-10 15:34:52.474  19410-19438/com.dk.dkmobile W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f1d8a0, error=EGL_SUCCESS
08-10 15:35:34.777  19410-19410/com.dk.dkmobile E/Error﹕ Single-Sign-On is only supported on Windows. Please specify a user name.
08-10 15:35:34.778  19410-19410/com.dk.dkmobile D/AndroidRuntime﹕ Shutting down VM
08-10 15:35:34.778  19410-19410/com.dk.dkmobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.dk.dkmobile, PID: 19410
    java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
            at com.dk.dkmobile.MainActivity$1.onClick(MainActivity.java:83)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-10 15:35:40.955  19410-19410/? I/Process﹕ Sending signal. PID: 19410 SIG: 9



MainActivity.java code:
public class MainActivity extends AppCompatActivity {

    Button loginbtn;
    TextView errorlbl;
    EditText edname, edpassword;
    Connection connect;
    PreparedStatement preparedStatement;
    Statement st;
    String ipaddress, db, username, password;


    @SuppressLint("NewApi")
    private Connection ConnectionHelper(String user,String password,String database, String server)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String ConnectionURL = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL = "jdbc:jtds:sqlserver://" + server + "/database name =" + database + ";user =" + user + ";password =" + password ;
            connection = DriverManager.getConnection(ConnectionURL);
        }
        catch (SQLException se)
        {
            Log.e("Error", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
            Log.e("Error",e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("Error",e.getMessage());
        }
        return connection;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loginbtn = (Button) findViewById(R.id.btn_login);
        errorlbl = (TextView) findViewById(R.id.lbl_error);
        edname = (EditText) findViewById(R.id.txt_username);
        edpassword = (EditText) findViewById(R.id.txt_password);

        ipaddress = "";
        username = "";
        password = "";
        db = "DKMobile";

        connect = ConnectionHelper(username,password,db,ipaddress);
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try{
                    connect = ConnectionHelper(username, password, db, ipaddress);
                    st = connect.createStatement();
                    ResultSet rs = st.executeQuery("Select * from DKMUsers where user_ID = '"+ edname.getText().toString()+"'and Password = '"+edpassword.getText().toString()+"'");
                    if(rs!=null && rs.next()){
                        Intent i = new Intent(MainActivity.this,MainActivity.class);
                        startActivity(i);
                    }
                    else {
                        errorlbl.setText("Sorry, wrong credentials!!!");

                    }

                }catch (SQLException e)
                {
                    errorlbl.setText(e.getMessage().toString());
                }
            }
        });
ankurdkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris HarteThaumaturgeCommented:
Your connection helper is returning null. Check you are using the correct ports in your ip address and add some semi-colons to server and password
 
CConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                            + "databaseName=" + database 
                            + ";user=" + user
                            + ";password=" + password + ";";

Open in new window


As a side note, it is considered bad practise to access a database directly from an app. Consider using a service to connect the two.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ankurdkAuthor Commented:
hello sir

i have checked the ip address and have changed the ConnectionURL statement. Still the problem remains the same.

thanks for giving your valuable time.
0
Chris HarteThaumaturgeCommented:
I missed a slash from databaseName, try this

CConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                             + "/databaseName=" + database 
                             + ";user=" + user
                             + ";password=" + password + ";";

Open in new window

0
Olajire Awoyemi AborisadeCommented:
sir.
am having the same problem thought am using MSSQL 2016
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.