0

I'm having an App mobile (Java) develop with Android Studio. I'm trying to connect to the SQL server with JDBC (JDBC 10.0.2 JRE 11). But I got an exception connection refused. My SQL Server is a container run on docker. I can connect to SQL Server on the container with SSMS but not in java.

My exception:

E/Sql Error:: The TCP/IP connection to the host 127.1.1.1, port 3366 has failed. Error: "Failed to connect to /127.1.1.1 (port 3366) from /127.0.0.1 (port 54430) after 427ms: isConnected failed: ECONNREFUSED (Connection refused). Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

Here is my code config JDBC.

    String server = "127.1.1.1";
    String database = "QuanLyTruyenHinh";
    String user = "QLTH";
    String pass = "QLTH@123";
    String port = "3366";

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try {

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String connectionUrl = "jdbc:sqlserver://" + server + ":" + port + ";database=" + database + ";user=" + user + ";password=" + pass+";";

        result = DriverManager.getConnection(connectionUrl);

    } catch (Exception ex) {
        Log.e("Sql Error: ", ex.getMessage());
    }

And here is inspect of the docker container:

[
{
    "Id": "56bc2e96eeb194d978fc6e6d48fde3a61e14f0ad8def582ea87e2cc6f7ac0457",
    "Created": "2022-04-12T19:09:01.4235105Z",
    "Path": "/opt/mssql/bin/permissions_check.sh",
    "Args": [
        "/opt/mssql/bin/sqlservr"
    ],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 4038,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2022-04-12T19:09:02.9319952Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "sha256:d78e982c2f2bdabbf34c3fd1b898ade9a25a22a5872ef23ea0c4fde1e6e3d7c8",
    "ResolvConfPath": "/var/lib/docker/containers/56bc2e96eeb194d978fc6e6d48fde3a61e14f0ad8def582ea87e2cc6f7ac0457/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/56bc2e96eeb194d978fc6e6d48fde3a61e14f0ad8def582ea87e2cc6f7ac0457/hostname",
    "HostsPath": "/var/lib/docker/containers/56bc2e96eeb194d978fc6e6d48fde3a61e14f0ad8def582ea87e2cc6f7ac0457/hosts",
    "LogPath": "/var/lib/docker/containers/56bc2e96eeb194d978fc6e6d48fde3a61e14f0ad8def582ea87e2cc6f7ac0457/56bc2e96eeb194d978fc6e6d48fde3a61e14f0ad8def582ea87e2cc6f7ac0457-json.log",
    "Name": "/infallible_shaw",
    "RestartCount": 0,
    "Driver": "overlay2",
    "Platform": "linux",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": {
        "Binds": [
            "SQLMobile:/var/opt/mssql/"
        ],
        "ContainerIDFile": "",
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "NetworkMode": "default",
        "PortBindings": {
            "1433/tcp": [
                {
                    "HostIp": "127.1.1.1",
                    "HostPort": "3366"
                }
            ]
        },
        "RestartPolicy": {
            "Name": "no",
            "MaximumRetryCount": 0
        },
        "AutoRemove": false,
        "VolumeDriver": "",
        "VolumesFrom": null,
        "CapAdd": null,
        "CapDrop": null,
        "CgroupnsMode": "host",
        "Dns": [],
        "DnsOptions": [],
        "DnsSearch": [],
        "ExtraHosts": null,
        "GroupAdd": null,
        "IpcMode": "private",
        "Cgroup": "",
        "Links": null,
        "OomScoreAdj": 0,
        "PidMode": "",
        "Privileged": false,
        "PublishAllPorts": false,
        "ReadonlyRootfs": false,
        "SecurityOpt": null,
        "UTSMode": "",
        "UsernsMode": "",
        "ShmSize": 67108864,
        "Runtime": "runc",
        "ConsoleSize": [
            41,
            175
        ],
        "Isolation": "",
        "CpuShares": 0,
        "Memory": 0,
        "NanoCpus": 0,
        "CgroupParent": "",
        "BlkioWeight": 0,
        "BlkioWeightDevice": [],
        "BlkioDeviceReadBps": null,
        "BlkioDeviceWriteBps": null,
        "BlkioDeviceReadIOps": null,
        "BlkioDeviceWriteIOps": null,
        "CpuPeriod": 0,
        "CpuQuota": 0,
        "CpuRealtimePeriod": 0,
        "CpuRealtimeRuntime": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "Devices": [],
        "DeviceCgroupRules": null,
        "DeviceRequests": null,
        "KernelMemory": 0,
        "KernelMemoryTCP": 0,
        "MemoryReservation": 0,
        "MemorySwap": 0,
        "MemorySwappiness": null,
        "OomKillDisable": false,
        "PidsLimit": null,
        "Ulimits": null,
        "CpuCount": 0,
        "CpuPercent": 0,
        "IOMaximumIOps": 0,
        "IOMaximumBandwidth": 0,
        "MaskedPaths": [
            "/proc/asound",
            "/proc/acpi",
            "/proc/kcore",
            "/proc/keys",
            "/proc/latency_stats",
            "/proc/timer_list",
            "/proc/timer_stats",
            "/proc/sched_debug",
            "/proc/scsi",
            "/sys/firmware"
        ],
        "ReadonlyPaths": [
            "/proc/bus",
            "/proc/fs",
            "/proc/irq",
            "/proc/sys",
            "/proc/sysrq-trigger"
        ]
    },
    "GraphDriver": {
        "Data": {
            "LowerDir": "/var/lib/docker/overlay2/2dbe5b89e006ca0989c0e7c8c369cc69e87954e4ed0eafb3715d4a63995cb17a-init/diff:/var/lib/docker/overlay2/854244ab887a98b3cccab9a0a3a2bccbe40f78257911bb41ffcdbc1ff0593de7/diff:/var/lib/docker/overlay2/25678837091a8032fc06a688a0372a80b203b7ebb293134ea274b076009edc50/diff:/var/lib/docker/overlay2/0d1703d5a04207ecea9d27ebd81e64c19ec13fab0a86b6349f8e0baa27d11bcc/diff:/var/lib/docker/overlay2/00b4458b9eefb8f2b87c041f1f60e9e621c77f20ff627cbc4420a2cec0f3d271/diff:/var/lib/docker/overlay2/043d50e96f42550ad5915086cdc3d2f15ea8724f1730dc7d7b73e44c0f3c9a4c/diff",
            "MergedDir": "/var/lib/docker/overlay2/2dbe5b89e006ca0989c0e7c8c369cc69e87954e4ed0eafb3715d4a63995cb17a/merged",
            "UpperDir": "/var/lib/docker/overlay2/2dbe5b89e006ca0989c0e7c8c369cc69e87954e4ed0eafb3715d4a63995cb17a/diff",
            "WorkDir": "/var/lib/docker/overlay2/2dbe5b89e006ca0989c0e7c8c369cc69e87954e4ed0eafb3715d4a63995cb17a/work"
        },
        "Name": "overlay2"
    },
    "Mounts": [
        {
            "Type": "volume",
            "Name": "SQLMobile",
            "Source": "/var/lib/docker/volumes/SQLMobile/_data",
            "Destination": "/var/opt/mssql",
            "Driver": "local",
            "Mode": "z",
            "RW": true,
            "Propagation": ""
        }
    ],
    "Config": {
        "Hostname": "56bc2e96eeb1",
        "Domainname": "",
        "User": "mssql",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": {
            "1433/tcp": {}
        },
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "ACCEPT_EULA=Y",
            "SA_PASSWORD=Minhtu@n2811",
            "MSSQL_PID=Developer",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd": [
            "/opt/mssql/bin/sqlservr"
        ],
        "Image": "d78e982c2f2b",
        "Volumes": null,
        "WorkingDir": "",
        "Entrypoint": [
            "/opt/mssql/bin/permissions_check.sh"
        ],
        "OnBuild": null,
        "Labels": {
            "com.microsoft.product": "Microsoft SQL Server",
            "com.microsoft.version": "15.0.4198.2",
            "vendor": "Microsoft"
        }
    },
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "222f468981a847ea0f798796e0d262d82cdaddfcdcb79216243ab17257b4df69",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "1433/tcp": [
                {
                    "HostIp": "127.1.1.1",
                    "HostPort": "3366"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/netns/222f468981a8",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "b84190964c07fb76905cebb9f8559c03cd7a35c5c771b35e558aaffc958a6521",
        "Gateway": "172.17.0.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "MacAddress": "02:42:ac:11:00:02",
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "b04f2954c8f39830217faffd7385d5d86586886b4fad47c52d5112e510b88b6c",
                "EndpointID": "b84190964c07fb76905cebb9f8559c03cd7a35c5c771b35e558aaffc958a6521",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:02",
                "DriverOpts": null
            }
        }
    }
}]

What keyword I can find the answer to my problem?

Dale K
  • 25,246
  • 15
  • 42
  • 71
TuiTenTuan
  • 45
  • 7
  • 2
    Please [reconsider using JDBC on Android](https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android). – CommonsWare Apr 12 '22 at 20:25
  • 1
    The keyword you want is "REST API layer" :) An intermediate Web service between a mobile app and a SQL database is how it's normally done. – Seva Alekseyev Apr 12 '22 at 20:27
  • Agree with the general "don't connect mobile devices to SQL Server" advice. Aside from the security issues putting an SQL Server on the public internet creates, the TDS protocol suffers badly under WAN-level latency and is not at all fault-tolerant. – AlwaysLearning Apr 12 '22 at 22:01
  • `127.1.1.1` is on the loopback network, which is non-routable. You can connect to this from SSMS because it's on the same machine. When you say Java can't connect to it is that because your Java app running in a device simulator? – AlwaysLearning Apr 12 '22 at 22:01

0 Answers0