Question

I'm using AsyncTask to connect to a remote SQL Server Database somehow the application stops at:

Connection conn = DriverManager.getConnection(url);

I tested the connection string in other java applications and it's working here's my AsyncTask Code:

private class ValidateUser extends AsyncTask<String, Integer, Integer> {

    @Override
    protected Integer doInBackground(String... params) {
        try {

            String url = getResources().getString(R.string.ConnectionString);
            String username = params[0];
            String password = params[1];
            Log.d("url", url);
            Log.d("username", username);
            Log.d("password", password);
            Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection(url);
            if (conn == null) // couldn't connect to server
            {
                return -1;
            }
            Statement statement = null;
            ResultSet resultSet = null;
            String sql = "select EMPLOYEE_ID from EMPLOYEES "
                    + "where EMPLOYEE_USERNAME = '" + username + "' "
                    + "and EMPLOYEE_PASS = '" + password + "'";
            statement = conn.createStatement();
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                return resultSet.getInt("EMPLOYEE_ID");
            }
        } catch (Exception e) {
            Log.d("BEZRA",e.getMessage());
            return -1;
        }
        return 0;
    }
}

No correct solution

OTHER TIPS

I got it, The code was fine. it just responded - after about 5 minutes of not responding- with an exception the it couldn't resolve server name, so I replaced it with the internal IP and it everything worked fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top