3

I try to create connection between android app and sql server. I have in main activity:

Connect cn = new Connect();
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +"databaseName=UNIVERSITY;";
cn.dbConnect(connectionUrl,"","");

And Connect class is:

import java.sql.*;
import android.util.Log;
import javax.sql.*;

public class Connect {

 public Connect() {}

    public boolean dbConnect(String db_connect_string,String db_userid, String db_password)
    {
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
            return true;

        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
}

I also add sqljdbc.jar file.but i see in logcat:

01-30 19:04:22.747: E/dalvikvm(1618): Could not find class 'javax.sql.XAConnection', referenced from method com.microsoft.sqlserver.jdbc.SQLServerConnection.close
01-30 19:04:22.747: W/dalvikvm(1618): VFY: unable to resolve instanceof 235 (Ljavax/sql/XAConnection;) in Lcom/microsoft/sqlserver/jdbc/SQLServerConnection;
01-30 19:04:22.757: D/dalvikvm(1618): VFY: replacing opcode 0x20 at 0x0037

I Google a lot but didn't find the reason of this error.please help.

sahar
  • 115
  • 2
  • 4
  • 15
  • The error message is occurring because the JDBC driver you are using requires JEE's javax.sql.XAConnection and Android most certainly does not include javax.sql.XAConnection, if you want to use JDBC direct to a SQL Server, you'll need to include all the libs your driver requires. That said, you almost never want to connect directly to a remote database server from a mobile app (you should consider creating an API that the mobile can access, usually over something simple like HTTP/JSON, Resty). Also, you're trying to connect to "localhost" as the server? Is SQL Server running on your Android? – Charlie Collins Jan 29 '13 at 16:27
  • excuse me I'm new in android.first time i tried to use php between Android app and sql server(already i use this method for mysql). but i cant implement it and after Google i see this way!would you help me? – sahar Jan 29 '13 at 16:59
  • Usually you don't connect to a SQL database directly from a mobile device. INSTEAD you'd usually use HTTP to access data from a mobile device. In order to use HTTP to access data, you need to create an API around your data that exposes it via HTTP. It's not that hard to do, and there are tools for helping with just about any platform/technology. Build your API BEFORE you worry about ANYTHING relating to Android. See: Flickr, Twitter, Google Data, and so on (all have REST, or REST-ish HTTP APIs, and we use THOSE from mobiles to do stuff, we don't connect directly over JDBC to Twitter, etc). – Charlie Collins Jan 29 '13 at 22:55
  • http://stackoverflow.com/questions/18672012/class-not-found-although-particular-jar-is-added-in-project-connecting-android/18673772#18673772 – C Sharper Oct 03 '13 at 08:01

0 Answers0