Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am newbie and i want to connect my app with a MSSql server. I tried a lot of solutions from google but i did nothing :/
I dont know good english.

What I have tried:

This is my main activity.

Java
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


public class MainActivity extends AppCompatActivity {

    SQLConnection connectionClass;
    EditText edtuserid,edtpass;
    Button btnlogin;
    ProgressBar pbbar;
    TextView txtView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtView = (TextView) findViewById(R.id.txtView1);
        connectionClass = new SQLConnection(); //the class file
        edtuserid = (EditText) findViewById(R.id.edtuserid);
        edtpass = (EditText) findViewById(R.id.edtpass);
        btnlogin = (Button) findViewById(R.id.btnlogin);
        pbbar = (ProgressBar) findViewById(R.id.pbbar);
        pbbar.setVisibility(View.GONE);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DoLogin doLogin = new DoLogin(); // this is the Asynctask
                doLogin.execute("");

            }
        });
    }

    public class DoLogin extends AsyncTask<String,String,String>
    {

        Boolean isSuccess = false;
        String userid = edtuserid.getText().toString();
        String password = edtpass.getText().toString();
        String z = "";
        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();

            if(isSuccess) {
                Intent i = new Intent(MainActivity.this, Connection.class);
                startActivity(i);
                finish();
            }

        }

        @Override
        protected String doInBackground(String... params) {
            if(userid.trim().equals("")|| password.trim().equals(""))
                z = "Please enter User Id and Password";
            else
            {
                try {
                    Connection con = SQLConnection.CONN();
                    if (con == null) {
                        z = "Error in connection with SQL server";
                        txtView.setText(z);
                    } else {
                        String query = "SELECT * FROM *****";
                        Statement stmt = con.createStatement();
                        ResultSet rs = stmt.executeQuery(query);

                        if(rs.next())
                        {

                            z = "Login successfull";
                            txtView.setText(z);
                            isSuccess=true;
                        }
                        else
                        {
                            z = "Invalid Credentials";
                            txtView.setText(z);
                            isSuccess = false;
                        }

                    }
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    z = "Exceptions" + "    "+ ex.getMessage() ;
                    txtView.setText(z);
                }
            }
            return z;
        }
    }
}

This is my Connection class

Java
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SQLConnection {
    private static final String LOG = "DEBUG";
    private static String ip = "*****";
    private static String port = "*****";
    private static String classs = "net.sourceforge.jtds.jdbc.Driver";
    private static String db = "*****";
    private static String un = "*****";
    private static String password = "*****";
    public static Connection CONN() {
        Connection conn = null;
        String ConnURL = null;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip +":"+port+";" + "databaseName=" + db + ";user=" + un + ";password=" + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException e) {
            Log.d(LOG, e.getMessage());
        } catch (ClassNotFoundException e) {
            Log.d(LOG, e.getMessage());
        }
        return conn;
    }
}
Posted
Comments
CHill60 10-Jul-18 8:08am    
So what happens when you run your code?
Member 13905541 10-Jul-18 9:30am    
I press "Log in" button and nothing. It's Loading all the time
Member 13905541 10-Jul-18 9:32am    
I wrote wrong the Code? or Something is not right with SQL server ?
David Crow 10-Jul-18 9:47am    
What happened when you stepped through each line of code using the debugger?
Member 13905541 10-Jul-18 10:02am    
Hmm I dont remember exactly cause now i am from my house and the PC with Android Studio is on my Office :/
The APK runs normally.When i dont put Acc/Pass show me "Please enter User Id and Password" but when i put a random or the correct Acc/Pass and press "Log In", Progress Bar start loading and never stop :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900