1

I am making an application for User Logging, when executing the emulator I can register users, verifying the users' stock in my Database, but when I execute the application on my phone, it does not connect to my Database.

This is my connection code to the Database:

public class ConnectionDB extends AppCompatActivity {

Connection connection;
    private static String ip = "192.168.1.9";  //"192.168.33.1";
    private static String port = "1433";
    private static String classs = "net.sourceforge.jtds.jdbc.Driver";
    private static String db = "PHOTOGO";
    private static String user = "mizrahijulio";
    private static String password = "julio1207";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        }

    //Connection method
    public Connection connectBD(){
         connection=null;
        try {
            //Acces Permition to DB
            StrictMode.ThreadPolicy policicy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policicy);

            Class.forName(classs).newInstance();
            connection= DriverManager.getConnection("jdbc:jtds:sqlserver://"+ip+":"+port+";databaseName="+db+";user="+user+";password="+password+";instance=SQLEXPRESS;");


        }catch(SQLException sq){
            Toast.makeText(getApplicationContext(),sq.getMessage(),Toast.LENGTH_SHORT).show();
        }
        catch (ClassNotFoundException cfe){
            Toast.makeText(getApplicationContext(),cfe.getMessage(),Toast.LENGTH_SHORT).show();
        }
        catch (Exception e){
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }

        return connection;
    }

    public Connection CloseConnection() throws SQLException {
try{
        if(connection!=null){
             connection.close();}
}
        catch(SQLException sq){
            Toast.makeText(getApplicationContext(),sq.getMessage(),Toast.LENGTH_SHORT).show();
        }
        return connection;
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Julio Mizrahi
  • 33
  • 1
  • 9
  • Possible duplicate of [The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption](https://stackoverflow.com/questions/18620869/the-driver-could-not-establish-a-secure-connection-to-sql-server-by-using-secure) – Feras Al Sous Aug 19 '18 at 07:23
  • Please read: [JDBC vs Web Service for Android](https://stackoverflow.com/q/15853367/295004) – Morrison Chang Aug 19 '18 at 07:29

2 Answers2

0

If your database is local, Your phone and your server computer must be connected to the same network. May be that is the problem, if your phone is connected to SIM network or to any other network which is not same as server computer network and you try to access it, it will not. When talking about your Emulator, it is obviously connected to the server network, hence it works.

Try it, may be that's the issue.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
0

If you have wifi and your computer and real device (mobile) is connected to the same wifi you can use NGROK software tool to run and debug your app in your real device.

Run the ngrok.exe and type ngrok http 80, it will give you an address like the below image.

Click

Just use the url in your app where you use your ip address. It is best to use.

Shaon
  • 2,496
  • 26
  • 27