0

I had added the network permissions and add jtds-1.3.1.jar to the libs driectory.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

But when I getConnection, it appears the exception message(android.os.NetworkOnMainThreadException) at DriveManager.getConnection. Does somebody know what I lose to set?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViews();
}

private void findViews() 
{

    try {
             Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
             con = DriverManager.getConnection(URL, USER, PASSWORD);
        }
        catch (Exception e)
        {
            Log.w("Error connection","" + e.getMessage());

        }
Akira Chen
  • 69
  • 10
  • Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – Mike M. Jul 25 '16 at 01:49
  • 1
    As its appears that you are trying to do JDBC directly from Android, please read: http://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android – Morrison Chang Jul 25 '16 at 02:09
  • I just want to test Android App directly connect to SQL Server. I will use web server to be the bridge of Android App and SQL Server. Now I just study the method of Android use JDBC connect to SQL Server. This problem is similar to How to fix Android.os.NetworkOnMainThreadException? But the difference between them, I use the jtds to do something. I will study this article, thanks Mike M. – Akira Chen Jul 25 '16 at 09:13
  • Another article's problem is similar to my MainThreadException problem。[Connecting android app to sql server using jtds](http://stackoverflow.com/questions/15276971/connecting-android-app-to-sql-server-using-jtds) – Akira Chen Jul 25 '16 at 09:27
  • Maybe the article is the solution: [Connect Android to SQL Server - Login App In Android](http://seotoolzz.com/android/android-login-app-with-mssql-server.php). I am studying. – Akira Chen Jul 25 '16 at 09:35

0 Answers0