当前位置: 代码迷 >> Android >> 从我的应用程序连接到本地SQL Server 2008
  详细解决方案

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

热度:76   发布时间:2023-08-04 12:30:11.0

我正在为我的朋友数据库类开发一个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)

用户名/密码是否正确? 您的SQL Server是否配置为“混合”身份验证模式? 用户帐户是否具有访问数据库的足够权限? 尝试将登录帐户设置为“ sysadmin”,这将赋予它最大的特权。

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

  相关解决方案