2 years ago

#70033

test-img

Daniel Reuveni

SQL Server - Login failed for user (Android Studio)

I have written code using JDBC to create registration page in Android Studio. I create database in SQL Server 2019. In addition I tried to connect from the code to MSSQL database and insert values, but while I run the app I'm getting the error:

login failed for user.

I have examined many options that can cause this error and I fixed them. For example: The server authentication is SQL Server authentication,the TCP option at the server is enabled, I have the relevant permissions of the user, also I check the if the firewall is the cause for this error. However, I continue to receive the error.

The connection class:

public class ConnectionClass {
    public static String ip= "***.***.*.*:1433"; //
    public static String user_name ="****";
    public static String password = "****";
    public static String database = "tests";
}

The Registeration class:

EditText email;
    EditText password;
    EditText confirmPassword;
    Button sign_up;
    EditText name;
    Connection connection1;
    Statement statement;
    TextView status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up_parent);
        email = (EditText) findViewById(R.id.editTextTextEmailAddress2);
        password = (EditText) findViewById(R.id.editTextTextPassword);
        confirmPassword = (EditText) findViewById(R.id.editTextTextPassword2);
        sign_up = (Button) findViewById(R.id.button);
        name =  (EditText) findViewById(R.id.editTextTextPassword3);
        status = (TextView) findViewById(R.id.status);

        sign_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ParentSignUp.registerUser().execute("");
            }
        });

    }



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

        String z ="";
        Boolean isSuccess = false;

        public registerUser() {
            super();
        }

        @Override
        protected void onPreExecute() {
            status.setText("Sending Data to Database");
        }

        @Override
        protected void onPostExecute(String s) {
            name.setText("");
            email.setText("");
            password.setText("");
            confirmPassword.setText("");
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                connection1 = connection_class(ConnectionClass.user_name.toString(), ConnectionClass.password.toString(),
                        ConnectionClass.database.toString(), ConnectionClass.ip.toString());
                if (connection1 == null) {
                    z = "Check Internet Connection";

                }
                else {
                    String sql = "INSERT INTO test (name,password, email) VALUES ('example', 'example', 'example')";
                    statement = connection1.createStatement();
                    statement.executeUpdate(sql);
                }
            }
            catch (Exception e) {
                isSuccess = false;
                z = e.getMessage();

            }
            return null;
        }
    }

    @SuppressLint("NewApi")
    public Connection connection_class (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 + ";user=" + user + ";"+"password=" + password + ";";
            connection = DriverManager.getConnection(connectionUrl);
        }catch (Exception e){
            Log.e("SQL Connection Error : ", e.getMessage());
        }

        return connection;

    }

android-studio

sql-server-2019

0 Answers

Your Answer

Accepted video resources