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:
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;
}
}