-4

I am trying to connect to my sql DB on my sql server, using the below code. But when I run it, the above error shows. I have added the permissions line to the manifest, but still no luck.

Any advice will be appreciated!

Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    Class.forName(driver).newInstance();
    //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
    String connString = "jdbc:jtds:sqlserver://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
    String username = "xxxxxx";
    String password = "xxxxxxxxxx";
    conn = DriverManager.getConnection(connString,username,password);
    Log.w("Connection","open");
    Statement stmt = conn.createStatement();
    ResultSet reset = stmt.executeQuery("select * from TableName");

    //Print the data to the console
    while(reset.next()){
        Log.w("Data:",reset.getString(3));
        //Log.w("Data",reset.getString(2));
    }
    conn.close();
} catch (Exception e) {
    Log.w("Error connection","" + e.getMessage());
}
HpTerm
  • 8,151
  • 12
  • 51
  • 67

1 Answers1

0

NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.

You should call sendfeedback method on asynctask then only above code will work. As webserver is taking lot of time to response main thread becomes unresponsive. To avoid it you should call it on another thread. Hence AsyncTask is better.

http://android-developers.blogspot.in/2009/05/painless-threading.html

ngrashia
  • 9,869
  • 5
  • 43
  • 58