0

I am trying to connect to SqlServer 2008r2 database whith Android.

It seems my connection string is not properly getting formed.

I am having following Error:

com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host 10.0.2.2, named instance 14graficali\mssqlserver20081433 failed. Error: "java.net.SocketException: Permission denied". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.

My SqlInstance:

enter image description here

Connection String which i formed:

String dbName = "AndroidDB";
     String serverip="10.0.2.2";
     String serverport="1433";
     String url = "jdbc:sqlserver://"+serverip+"\\14GRAFICALI\\MSSQLSERVER2008"+serverport+";databaseName="+dbName+"";

Whole Code in which i am implementing it:

public class MainActivity extends Activity {


     String dbName = "AndroidDB";
     String serverip="10.0.2.2";
     String serverport="1433";
     String url = "jdbc:sqlserver://"+serverip+"\\14GRAFICALI\\MSSQLSERVER2008"+serverport+";databaseName="+dbName+"";

    @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);                   

            Statement statement=conn.createStatement();
            ResultSet resultSet=statement.executeQuery("select * from UserMaster");
            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;
    }

}
4

2 回答 2

1

Go to your AndroidManifest.xml file under your android project, open it and add this line :

 <uses-permission android:name="android.permission.INTERNET"/>
于 2013-09-07T06:42:40.553 回答
1

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:

Class not found although particular jar is added in project (android to sqlserver without webservice)

Please follow the stages and i am sure you will overcome all the errors.

于 2013-09-07T13:28:37.927 回答