Android AsyncTask在获取连接时迷失了方向

时间:2014-02-28 18:48:09

标签: java android sql-server android-asynctask database-connection

我正在使用AsyncTask连接到远程SQL Server数据库 不知何故,应用程序停在:

Connection conn = DriverManager.getConnection(url);

我测试了其他java应用程序中的连接字符串,它正在运行 这是我的AsyncTask代码:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我明白了,代码很好。 它只是响应 - 大约5分钟没有响应 - 一个例外,它无法解析服务器名称,所以我用内部IP替换它,一切正常。