连接字符串形成为Android到SQLServer的连接 [英] Connection String formation for Android to SQLServer Connectivity

查看:249
本文介绍了连接字符串形成为Android到SQLServer的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图2008R2数据库连接到SqlServer的蒙山Android系统。

这似乎不正确得到我的形成连接字符串。

我有以下错误:


  

com.microsoft.sqlserver.jdbc.SQLServerException:向连接
  主机10.0.2.2,命名实例14graficali \\ mssqlserver20081433失败。
  错误:java.net.SocketException异常:权限被拒绝。验证
  服务器和实例的名称并确认是否有防火墙阻止UDP
  流量端口1434对于SQL Server 2005或更高版本,请检查
  SQL Server Browser服务在主机上运行。


我SQLINSTANCE:

我形成了

连接字符串:

 字符串DBNAME =AndroidDB;
     字符串SERVERIP =10.0.2.2;
     字符串serverport =1433;
     字符串URL =的jdbc:SQLSERVER://+ SERVERIP +\\\\ \\\\ 14GRAFICALI MSSQLSERVER2008+ serverport +的databaseName =+ DBNAME +;

全code中,我实现它:

 公共类MainActivity延伸活动{
     字符串DBNAME =AndroidDB;
     字符串SERVERIP =10.0.2.2;
     字符串serverport =1433;
     字符串URL =的jdbc:SQLSERVER://+ SERVERIP +\\\\ \\\\ 14GRAFICALI MSSQLSERVER2008+ serverport +的databaseName =+ DBNAME +;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        TextView的tvData =(的TextView)findViewById(R.id.tvSelectedData);        尝试{
            的Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver)的newInstance()。
            康涅狄格州的连接=的DriverManager.getConnection(URL);            声明语句= conn.createStatement();
            ResultSet中的resultSet = Statement.executeQuery的(从UserMaster选择*);
            而(resultSet.next()){
                tvData.setText(数据1:+ resultSet.getString(1)+数据2:+ resultSet.getNString(2));
            }        }赶上(例外五){
            e.printStackTrace();
            tvData.setText(e.getMessage());
        }    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }}


解决方案

有需要时,我们连接在Android的情况下要foolwed几步,直接一个SQLServer。

我刚才提到的每一个详细的步骤在我的答案在这里:

<一个href=\"http://stackoverflow.com/questions/18672012/class-not-found-although-particular-jar-is-added-in-project?answertab=active#tab-top\">Class未发现虽然特别罐子项目添加

请跟随阶段,我相信你一定会克服所有的错误。

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

}

解决方案

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

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

这篇关于连接字符串形成为Android到SQLServer的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆