从我的应用程序连接到本地SQL Server 2008

时间:2018-09-16 15:18:02

标签: android sql sql-server database-connection

我正在为我的朋友数据库类开发一个Android应用程序,但是有点束缚。我在建立连接时遇到麻烦。有人可以协助我吗?我有一个本地数据库,我对MS SQL Server不太了解。这是信息:

ip = "Ip address"; // i am inserting IPV4 IP of computer in this
        db = "testing";
        un = "xee";
        pass = "1995";
        port = "1433";

    @SuppressLint("NewApi")
    public Connection connectionclass(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").newInstance();
            ConnectionURL = "jdbc:jtds:sqlserver://" + ip +":"+port+";"+ "databaseName=" + db + ";user=" + un + ";password="+ password + ";";
        }
        catch (SQLException se)
        {
            Log.e("error here 1 : ", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
            Log.e("error here 2 : ", e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("error here 3 : ", e.getMessage());
        }
        return connection;
    }
}

尝试连接时出现错误

E/error here 1 :: Network error IOException: failed to connect to /***.***.*.*** (port 1433) from /:: (port 35033): connect failed: ECONNREFUSED (Connection refused)

1 个答案:

答案 0 :(得分:0)

用户名/密码是否正确? 您的SQL Server是否配置为“混合”身份验证模式? https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/change-server-authentication-mode?view=sql-server-2017 用户帐户是否具有足够的权限来访问数据库?尝试将登录帐户设置为“ sysadmin”,这将赋予它最大的特权。

检查防火墙(不是本地数据库)