0

I am a beginner to android development. now am trying to connect with MS SQL 2008 R2 via emulator.MS SQL 2008 R2 running on another (server) machine. Am getting the following error:

The TCP/IP connection to the host xxx.xxx.xx.x\SQL2008R2DEV has failed; 
error: null;
verify connection properties.
make sure that instance of SQL server running on the host and accepting TCP/IP connection to the port are not blocked by Windows Firewall.

Note I have done this in java application. and also I can access my database. but why cannot in android program? have any idea?

NOTE: BOTH SERVER AND MY PC ARE CONNECTED VIA LAN.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
  • In your previous question, you are advised not to use JDBC. In this question you are asking the same thing. We don't welcome asking same question multiple times giving different titles but explaining same problem. – NewUser Mar 04 '13 at 09:23
  • @Dibya,sorry am new to stackoverflow, so I don't know how to use it. – want to delete Mar 04 '13 at 09:54

3 Answers3

5

Yes it is possible to connect to MSSQL from android emulator. For example if the other system is having IP 192.168.1.2 then you can very use the same IP in your emulator to connect to the database. Anyway using JDBC in android is not encouraged. This link will explain why it not advisable to use JDBC in Android. This is how you can connect to MSSQL database using JDBC:

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        Connection conn =DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=msdibya;user=dibya;password=dibya;");                   

        System.out.println("connected");
        Statement statement=conn.createStatement();
        ResultSet resultSet=statement.executeQuery("select * from [user]");
        while(resultSet.next()){
            System.out.println(" "+resultSet.getString(1)+" "+resultSet.getNString(2));
        }

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

I hope you have the proper jar files. Don't forget to import them.

Community
  • 1
  • 1
NewUser
  • 3,729
  • 10
  • 57
  • 79
1

You can't access remote database directly only way to access is through web services in android.Also any network related task don't write on main thread use Asynctask for that.If post your code how you are accessing will be more useful.

Narendra
  • 609
  • 1
  • 12
  • 28
0

It is not advisable to create MSSQL connection from android, its better to create web service in server and use out put of web service in android.

Mehdei
  • 245
  • 1
  • 4
  • 10