C Sharper
C Sharper

Reputation: 8626

Class not found exception while class is added

I am doing SqlServer connectivity in Android.

I am including all the necessary jar files for it.

enter image description here

Buildpath Snap:

enter image description here

Error Line:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

ORDER & EXPORT:

enter image description here

Edit

package com.example.sqlservercall;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {


    String url="jdbc:sqlserver://10.0.2.2;instance=14GRAFICALI\\MSSQLSERVER2008;databaseName=AndroidDB;integrated security=true";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TextView tvData=(TextView)findViewById(R.id.tvSelectedData);

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Connection conn =DriverManager.getConnection(url);                   

            System.out.println("connected");
            Statement statement=conn.createStatement();
            ResultSet resultSet=statement.executeQuery("select * from AndroidDB");
            while(resultSet.next()){
                tvData.setText(" Data1 : "+resultSet.getString(1)+"  Data 2 : "+resultSet.getNString(2));
            }

        } catch (Exception e) {
            e.printStackTrace();
            tvData.setText(e.getMessage());
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Exception Detailed:

enter image description here

NEW Error afeter Checking jars in order and export:

Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator$1;
[2013-09-06 18:24:04 - SQLServerCall] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator$1;
[2013-09-06 18:24:23 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator$1;
[2013-09-06 18:24:23 - SQLServerCall] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator$1;

Upvotes: 3

Views: 801

Answers (3)

MDroid
MDroid

Reputation: 550

The Error is of "Multiple Dex File" so must be you have add Same jar multiple times as per your Screenshot see Buildpath Snap: there are two sqljdbc.ja**r. keep only one(keep latest one,remove other) then move to **ORDER & EXPORT: and select mostly all check box. like Android support version 4 also. It will solve your issue..

Upvotes: 1

Simon Forsberg
Simon Forsberg

Reputation: 13331

Make sure that the libraries are in "Android Private Libraries" and add "Android Private Libraries" in Order and Export tab.

It should be enough having the libraries inside the libs directory, you don't need to add them to the build path since all files in the libs directory is automatically added to the build path (through "Android Private Libraries").

I also believe that you are attempting to instantiate the wrong class, try with the class name com.microsoft.sqlserver.jdbc.SQLServerDriver (I noticed in your screenshot that the real package name is com.microsoft.sqlserver.jdbc and not com.microsoft.jdbc.sqlserver)

Edit: (Updated answer after your updates)

I assume both sqljdbc.jar and sqljdbc4.jar include the same classes, if that is the case you can only include one of them in your project.

Upvotes: 1

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31225

From what I can see from your screenshot, you should replace :

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")

with :

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

Upvotes: 1

Related Questions