2014-07-14 2 views
-4

아래 코드를 사용하여 SQL Server에서 내 sql DB에 연결하려고합니다. 하지만 실행하면 위의 오류가 표시됩니다. 매니페스트에 사용 권한 줄을 추가했지만 행운은 없습니다.android.os.on 메인 스레드 예외

어떤 조언을 주시면 감사하겠습니다!

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()); 
} 
+0

왜 이미 -2가 있습니까? 이것에 대한 이유는 무엇입니까? – user3836282

+3

당신은 한번 예외를 찾기 위해 노력하지 않았기 때문에. 어쨌든, 여기에 간다 : http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception –

+1

문제는 메인 스레드에서 네트워크에 액세스하는 것인가? –

답변

0

NetworkOnMainThreadException : 응용 프로그램의 주 스레드에서 네트워킹 작업을 수행하려고 할 때 발생합니다 예외입니다.

asynctask에서 sendfeedback 메서드를 호출하면 위의 코드 만 작동합니다. 웹 서버가 응답 시간을 많이 차지함에 따라 주 스레드는 응답하지 않게됩니다. 이를 피하려면 다른 스레드에서 호출해야합니다. 따라서 AsyncTask이 좋습니다.

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