如何连接到Android模拟器使用JDBC MSSQL服务器? [英] How to connect to mssql server using jdbc in android emulator?

查看:473
本文介绍了如何连接到Android模拟器使用JDBC MSSQL服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者到Android的发展。现在正在尝试与另一(服务器)的机器上运行MS SQL 2008 R2通过emulator.MS SQL 2008 R2连接。我收到以下错误:

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.

请注意我在Java应用程序中做到了这一点。而且我可以访问我的数据库。
但为什么不能在Android程序?
有什么想法?

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

请注意:服务器和我的电脑已连接通过LAN

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

推荐答案

是的,它可以连接从Android模拟器到MSSQL。例如,如果其他系统是有IP 192.168.1.2 ,那么你可以很使用相同的IP在仿真器连接到数据库。反正在Android中使用JDBC不鼓励。 link 将解释为什么它不建议在Android中使用JDBC 这是如何使用JDBC连接MSSQL数据库:

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

我希望你有正确的jar文件。不要忘了将其导入。

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

这篇关于如何连接到Android模拟器使用JDBC MSSQL服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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