3

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/ActivityCorrelator1ドル;
[2013年09月06日 18:24:04 - SQLServerCall] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator1ドル;
[2013年09月06日 18:24:23 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator1ドル;
[2013年09月06日 18:24:23 - SQLServerCall] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/microsoft/sqlserver/jdbc/ActivityCorrelator1ドル;
asked Sep 6, 2013 at 12:07
16
  • Is it checked under the Order and Export tab? Not sure if it might need to be in there too. Also, maybe just clean the project. Commented Sep 6, 2013 at 12:09
  • Try Project -> Clean once. Eclipse sometimes behaves wierd Commented Sep 6, 2013 at 12:10
  • no cleaning project also does not helps Commented Sep 6, 2013 at 12:12
  • 1
    you should not connect from android to an SQLServer... why would you want to do that? Commented Sep 6, 2013 at 12:46
  • 2
    you probably need to use only one of these 2 libraries Commented Sep 6, 2013 at 12:58

3 Answers 3

1

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.

answered Sep 6, 2013 at 12:13
Sign up to request clarification or add additional context in comments.

5 Comments

@ShrimantBajiraoPeshawe-I see updated answer, I believe you are trying with the wrong classname. Through your screenshot, I can see the real package name is com.microsoft.sqlserver.jdbc but you switched sqlserver and jdbc in your code.
yeah its true that it was wrongly written, i changed it as u pointed out, but unfortunatly same error again
@ShrimantBajiraoPeshawe-I please update your question and add more code along with the detailed exception that you get.
@ShrimantBajiraoPeshawe-I Thanks for calling me Sir :) Updated my answer again.
sir i have done the connection sucessfully, and have mentioned the steps in the link which i provided in my answer :) Thanx For your support :)
1

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")
answered Sep 6, 2013 at 12:25

1 Comment

i changed it as u pointed out, but unfortunatly same error again
1

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..

answered Sep 6, 2013 at 13:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.