Search code examples
javac#androidsql-servermssql-jdbc

How To Improve SQL Connection


Hello every body I'm working on application which is sends data from android app to MS-SQL server and main windows software in C#, which is receives data from MS-SQl server. The problem is the programs takes too time to build a connection especially in android app some times it crash the app. By the way the Internet speed some times goes week in our country. I searched for a solution but not found in internet and I cannot figure out any way to solve it. And I see the Viber, Watsapp, Massenger ... etc it sends data instantly or synchronously even if Internet speed is week. So can I get some help and suggestion. And there is a connection Helper method :


public Connection connections(){

    IP="www.examlple.net";
    DB="DB_test";
    DBUserName="admin";
    DBPassword="*****";    
    StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection=null;
    String connectionURL=null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        connectionURL ="jdbc:jtds:sqlserver://"+IP+";DatabaseName="+DB+";integratedSecurity=true;user="+DBUserName+";password="+DBPassword;

        connection = DriverManager.getConnection(connectionURL);

    } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    return connection;
}

Solution

  • For the sake of completeness, with JTDS you can set both a loginTimeout and a socketTimeout on a connection string. Refer to the remarks on these here.

    But as others have said, you should go through a web API of some sort. Do you really want to expose your SQL server to the internet?

    Also, I just noticed you have specified integrated security=true, and you have also specified a username and password. You can't do that. One is for windows auth (integrated security) and the other is for SQL auth (user and password). You would have to use a username and password. But don't. Don't do this. go through a web API.