IOException: ... Connection timed out

Viewed 72

I am new to Android, and I tried copying a code to study how to connect my SQL database with my app. However, I get this error

Network error IOException: failed to connect to /169.254.57.155 (port 1433) from /:: (port 45212): connect failed: ETIMEDOUT (Connection timed out)"

Here is my ConSQL.java file:

`

package com.example.connectsql_trial;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConSQL {

    @SuppressLint("NewApi")
    public static Connection conclass(){
        String ip="169.254.57.155",port="1433",db="Trialdb1",username="sa",password="art121499";

        StrictMode.ThreadPolicy a = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(a);
        Connection con = null;
        String ConnectURL = null;
        try{
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectURL = "jdbc:jtds:sqlserver://"+ip+";"+"databasename="+db+";user="+username+";password="+password+";";
            con = DriverManager.getConnection(ConnectURL);
        }
        catch (SQLException se) {
            Log.e("Error SQLException", se.getMessage());
            Log.e("Error SQLException", String.valueOf(se.getErrorCode()));
        }
        catch (ClassNotFoundException e) {
            Log.e("Error Class not found", e.getMessage());
        }
        catch(Exception e){
            Log.e("Error is", e.getMessage());
        }
        return con;
    }
}

`

1 Answers

Likely a network problem, but this is actually a good thing because it made you post here. Do NOT access a database like this. Its totally insecure. It forces you to put the password to the db in the client, which means its trivially accessed by anyone with a copy of your app. You have 0 security this way. Instead, you should use a webservice as a middle man between your app and the db, so your password doesn't have to be globally accessible.

Related