0

I have created a database in Azure SQl with named as USERS ,and I want to access a column named as "FullName" Loaded jar: sqljtdc4.jar I have searched over internet and read documentations and finally implemented this but having this unknown error

My Attempt:

String hostName = "ABC.database.windows.net"; 
        String dbName = "myfirst"; 
        String user = "username"; 
        String password = "abcde"; 

        String url = String.format("jdbc:sqlserver://%s:1433;database=%s;user=%s;password=%s;encrypt=true;"
                + "hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);

//I have tried this replacing database  with databaseName/Database/DatabaseName

        Connection connection = null;

        try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            connection = DriverManager.getConnection(url);
            String SQL = "select * from dbo.USERS";


            try (Statement statement = connection.createStatement();
                 ResultSet resultSet = statement.executeQuery(SQL)) {

                // Print results from select statement
                while (resultSet.next())
                {
                    Toast.makeText(this,resultSet.getString("FullName"), Toast.LENGTH_SHORT).show();
                }
                connection.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

But getting this error:

 W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
2019-08-06 21:27:52.400 15125-15125/com.abc.abc W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
2019-08-06 21:27:52.400 15125-15125/com.abc.abc W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
2019-08-06 21:27:52.401 15125-15125/com.abc.abc W/System.err:     at java.net.InetAddress.getByName(InetAddress.java:1108)
2019-08-06 21:27:52.401 15125-15125/com.abc.abc W/System.err:     at java.net.InetSocketAddress.<init>(InetSocketAddress.java:235)
2019-08-06 21:27:52.401 15125-15125/com.abc.abc W/System.err:     at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:356)
2019-08-06 21:27:52.402 15125-15125/com.abc.abc W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1034)
2019-08-06 21:27:52.402 15125-15125/com.abc.abc W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
2019-08-06 21:27:52.403 15125-15125/com.abc.abc W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
2019-08-06 21:27:52.403 15125-15125/com.abc.abc W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
2019-08-06 21:27:52.404 15125-15125/com.abc.abc W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:569)
2019-08-06 21:27:52.404 15125-15125/com.abc.abc W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:237)

1 Answers1

0

It's a classic issue for Android App, you can not directly perform a networking operation in the main thread.

There are two SO threads as below to get the same issue with yours, you can refer to them to solve it.

  1. Error StrictMode$AndroidBlockGuardPolicy.onNetwork
  2. How do I fix android.os.NetworkOnMainThreadException?

One way is as below to add the two lines.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Alternatively to create a new thread to run the code in it.

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43