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ドル;
-
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.Scott Helme– Scott Helme2013年09月06日 12:09:36 +00:00Commented Sep 6, 2013 at 12:09
-
Try Project -> Clean once. Eclipse sometimes behaves wierdJatin– Jatin2013年09月06日 12:10:08 +00:00Commented Sep 6, 2013 at 12:10
-
no cleaning project also does not helpsC Sharper– C Sharper2013年09月06日 12:12:30 +00:00Commented Sep 6, 2013 at 12:12
-
1you should not connect from android to an SQLServer... why would you want to do that?Ovidiu Latcu– Ovidiu Latcu2013年09月06日 12:46:58 +00:00Commented Sep 6, 2013 at 12:46
-
2you probably need to use only one of these 2 librariesnjzk2– njzk22013年09月06日 12:58:14 +00:00Commented Sep 6, 2013 at 12:58
3 Answers 3
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.
5 Comments
com.microsoft.sqlserver.jdbc but you switched sqlserver and jdbc in your code.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")
1 Comment
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..