0

I'm trying to connect my Android app to my local machine database MS SQL. This is my connection string:

jdbc:jtds:sqlserver://localhost:1433/DajSve;encrypt=false;user=root;password=null;instance=FALE//SQLEXPRESS;

And I got this error:

java.sql.SQLException: Network error IOException: failed to connect to localhost/127.0.0.1 (port 1433): connect failed: ECCONREFUSED (Connection refused)

I tried a lot of things. Changed a connection string a lot of times, enabled TCP/IP and set it to port 1433, also turned off firewall but nothing helped. I'm always getting the same SQL Exception. I'm using SQL Server 2016..

  try {

        Class.forName(className).newInstance();
        connection = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/DajSve;encrypt=false;user=root;password=null;instance=FALE//SQLEXPRESS;");

        System.out.print("Uspjesno spojeni na bazu");
        Statement stmt = connection.createStatement();
        ResultSet reset = stmt.executeQuery("select * from Korisnik");

        if (!reset.isBeforeFirst() ) {
            System.out.println("nema podataka");
        }else{
            System.out.println("ima podataka");
        }

    } catch (SQLException e) {
        e.printStackTrace();
        System.out.print("Greska pri spajanju na bazu");
    } catch (ClassNotFoundException e){
        System.out.print("Greska - klasa nije pronađena");
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

I know there is a lot of similar questions but none of the answers have helped

1 Answers1

1

failed to connect to localhost/127.0.0.1

Your Android device isn't running a SQL Server. Give the actual server address on your network (but actually don't because you shouldn't be storing database credentials as part of your app, plus persistent JDBC connections kill battery life).

It's not a local database when your code exists on a remote device

You will actually want to create a new application that is acts as a backend API between your Android app and the database, where you can expose functions that connect to your database and parse the results there rather than doing it all in the Android app

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245