1

I've made a SQL Server and managed to form a database. Implemented everything in my Android Studio app and opened the app on my phone.

When I access the database from the same WiFi network where the database is formed, everything works, but when I access the database from another WiFi network I get the exception:

java.sql.SQLException: Network error IOException: Host unreachable

Can you make the server accessible from every other network apart from the network that the database is connected to? Or do I have to do something else to make it accessible from everywhere?

Things I've already done:

  • Every SQL services are up and running.
  • Shared Memory, Named Pipes and TCP/IP are enabled.
  • Remote server connections is Allowed on my database.
  • New account is created (I don't use the sa account).
  • I've made an exception in the firewall for the port (1433).

I will add the code from my app if that helps in any way.

import android.Manifest;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {
    TextView moreGames;
    String b;
    private static String ip = "xxx.xxx.x.x";
    private static String port = "1433";
    private static String Classes = "net.sourceforge.jtds.jdbc.Driver";
    private static String database = "xxxx";
    private static String username = "xxxxxx";
    private static String password = "xxxx";
    private static String url = "jdbc:jtds:sqlserver://" + ip + ":" + port + "/" + database;
    private Connection connection = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        moreGames = findViewById(R.id.moreGames);
        ActivityCompat.requestPermissions(this, new String[]{(Manifest.permission.INTERNET)}, PackageManager.PERMISSION_GRANTED);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            Class.forName(Classes);
            connection = DriverManager.getConnection(url, username, password);
            moreGames.setText(R.string.NewGame);
        } catch (ClassNotFoundException e){
            e.printStackTrace();
            Toast.makeText(this, "Error somewhere.", Toast.LENGTH_SHORT).show();
        } catch (SQLException e){
            b = String.valueOf(e);
            Toast.makeText(this, b, Toast.LENGTH_LONG).show();
        }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Please [reconsider your use of JDBC in an Android app](https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android). – CommonsWare Sep 18 '21 at 10:56
  • Talk to your DBA and network operations team if you want to make your database publicly accessible. They likely tell you "NO!", because generally this is considered unsafe, as is storing database credentials in your Android application. As CommonsWare suggested, use a webservice to mediate between your application and the database instead of exposing the database to the internet. – Mark Rotteveel Sep 18 '21 at 11:36
  • I completely understand. Thanks for the help guys. – Dejan Junduk Kuzmanovic Sep 20 '21 at 05:09

0 Answers0