Search code examples
androidsqljtds

JTDS connect to remote server fail


don't know why, the app when connecting to the SQL Server that is on the same network as the device, I can connect to it without any problems, but now I need to connect from the device over the internet to the SQL Server, isn't working, don't know why. I have a button to test the connection and it will call a method that contains this, the method is on background (AsyncTaskRunner)

try
{
    String host, port, dbname, user, password, instance;
    host = _editTextHost.getText().toString();
    port = _editTextPort.getText().toString();
    instance = _editTextInstance.getText().toString();
    dbname = _editTextDbName.getText().toString();
    user = _editTextUser.getText().toString();
    password = _editTextPass.getText().toString();
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    String conString;
    if (TextUtils.isEmpty(port))
    {
        conString = "jdbc:jtds:sqlserver://" + host + ";databaseName=" + dbname + ";instance=" + instance;
    }
    else
    {
        conString = "jdbc:jtds:sqlserver://" + host + ":" + port + ";databaseName=" + dbname + ";instance=" + instance;
    }
    Connection con;
    Class.forName(driver);
    con = DriverManager.getConnection(conString, user, password);
    con.close();
    conSuccess = true;
}
catch (Exception e)
{
    e.printStackTrace();
    Log.e("SQLConfig", "Fail to connect");
    Log.e("SQLConfig", e.toString());
    Log.e("SQLConfig", e.getMessage());
}
return null;

When I do try to connect to the SQL Server on the same network works without any problems, but when I activate the 4g on the device I allways get the same error, that it can't find the instance. But if I connect to the server through the "SQL Server Management Studio" using the same information I can connect to the server without any problems.

I'm using the jtds driver, 1.3.1.

What could be doing this? Thanks

P.S. I all rdy have read some stuff about webservice, but I want to remove this option for now out of the picture

Edit 1: To clarify, I can connect to the server using the credentials on the version of Windows CE of the program or SQL Server Management Studio. When I put the outside IP and all the require information it connects to the server, it not connect on the Android only


Solution

  • Well by changing the conString a little I was able to connect without any problems either from the localnetwork or the internet.

    if (TextUtils.isEmpty(port))
    {
        conString = "jdbc:jtds:sqlserver://" + host + ";databaseName=" + dbname + ";instance=" + instance;
    }
    else
    {
        conString = "jdbc:jtds:sqlserver://" + host + ":" + port + ";databaseName=" + dbname + ";instance=" + instance;
    }
    

    To

    if (TextUtils.isEmpty(port))
    {
        conString = "jdbc:jtds:sqlserver://" + host + "/" + instance + ";DatabaseName=" +dbname;
    }
    else
    {
        conString = "jdbc:jtds:sqlserver://" + host + ":" + port + "/" + instance + ";DatabaseName=" + dbname;
    }
    

    Now works without any problems either using the public host or the localnetwork to access the db.