Reputation: 8626
I am doing SqlServer2008 r2 connectivity with android.
My code is as follows:
public class MainActivity extends Activity {
String url="jdbc:sqlserver://localhost;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();
}
}
This code is not working and giving me error on line:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Error:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
But when i checked this path, file was there with above path.
Have a look at it:
What can be the problem?
Please help me.
EDIT
Javabuildpath:
Libs:
Upvotes: 2
Views: 5604
Reputation: 8626
There are Few Steps That need to be foolwed in case when we are connecting Android to directly a SQLServer.
I have mentioned each and every detailed steps in my answer over here:
Please follow the stages and i am sure you will overcome all the errors.
Upvotes: 0
Reputation: 18933
The CLASSPATH variable is the search string that Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not listed in your CLASSPATH variable, you receive the following error message when you try to load the driver: java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver
The JDBC driver is not part of the Java SDK. If you want to use it, you must set the classpath to include the sqljdbc.jar file or the sqljdbc4.jar file. If the classpath is missing an entry for sqljdbc.jar or sqljdbc4.jar, your application will throw the common "Class not found" exception.
The sqljdbc.jar file and sqljdbc4.jar file are installed in the following location:
<installation directory>\sqljdbc_<version>\<language>\sqljdbc.jar
<installation directory>\sqljdbc_<version>\<language>\sqljdbc4.jar
The following is an example of the CLASSPATH statement that is used for a Windows application:
CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar
The following is an example of the CLASSPATH statement that is used for a Unix/Linux application:
CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_4.0/enu/sqljdbc.jar
You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either sqljdbc.jar or sqljdbc4.jar.
Upvotes: 1
Reputation: 18702
Make sure that the jar which contains SQLServerDriver class in your classpath as explained here.
If you are using Eclipse, right click on your project, then Java Build Path, under Libraries tab, check if the jar is there and no other version of the same jar exists.
Upvotes: 1
Reputation: 23655
According to this thread on the microsoft msdn forum using a different JDBC driver should work. I haven't tested it myself though.
Upvotes: 1