连接本地SQL Server数据库工作在模拟器,但未能真实设备 [英] Connection to local SQL Server database works on emulator but fails on real device

查看:692
本文介绍了连接本地SQL Server数据库工作在模拟器,但未能真实设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到当前我的本地机器上托管的SQL Server数据库。我使用下面的code连接:

 公共类DBCONNECT {    私有静态挥发DBCONNECT INSTANCE = NULL;
    字符串URL =;
    康涅狄格州的连接= NULL;
    声明语句= NULL;
    ResultSet中的resultSet = NULL;    私有数据库连接(){    }    公共静态数据库连接的getInstance(){
        如果(INSTANCE == NULL){
            同步(DBConnect.class){
                实例=新的数据库连接();
            }
        }
        返回实例;
    }    公共无效setConnection(字符串数据库名,用户名字符串,字符串密码){
        尝试{
            的Class.forName(net.sourceforge.jtds.jdbc.Driver)的newInstance()。
            URL =的jdbc:JTDS:SQLSERVER://192.168.0.81:1433;实例名= SQLEX $ P $干燥综合征;数据库名=+数据库名+; integratedSecurity =真;用户=+用户名+;密码=+密码;
            康恩=的DriverManager.getConnection(URL);
        }赶上(例外五){
            e.printStackTrace();
        }
    }

这适用于我的Genymotion模拟器,因为我还执行的选择,看到显示的结果。

我使用的驱动程序JTDS做到这一点。

然而,当我尝试用相同的Wi-Fi网络,它不工作的实际设备,我得到一个连接错误。

任何想法,为什么我收到此?

这是我的错误日志:

 值java.sql.SQLException:网络错误IOException:无法连接到/192.168.0.81 90000ms后(1433端口)
8月7日至18日:29:09.465 27413-27514 / ordermanager.sentosa.ro.ordermanager W / System.err的:在net.sourceforge.jtds.jdbc.ConnectionJDBC2&下;初始化>(ConnectionJDBC2.java:410)
8月7日至18日:29:09.466 27413-27514 / ordermanager.sentosa.ro.ordermanager W / System.err的:在net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
8月7日至18日:29:09.466 27413-27514 / ordermanager.sentosa.ro.ordermanager W / System.err的:在java.sql.DriverManager.getConnection(DriverManager.java:175)
8月7日至18日:29:09.467 27413-27514 / ordermanager.sentosa.ro.ordermanager W / System.err的:在java.sql.DriverManager.getConnection(DriverManager.java:140)
8月7日至18日:29:09.467 27413-27514 / ordermanager.sentosa.ro.ordermanager W / System.err的:在ordermanager.sentosa.ro.ordermanager.utils.DBConnect.setConnection(DBConnect.java:39)


解决方案

你的问题有两种可能的解决方案,我可以看到:


  1. 您使用的是Genymotion模拟器这是一个不同的网络上可能工作

  2. 检查,如果您的SQL Server数据库处于在线模式下访问通过设置这样的:

    ALTER DATABASE数据库名网上订


I am trying to connect to an SQL Server database that is currently hosted on my local machine. I am using the following code to connect:

public class DBConnect {

    private static volatile DBConnect INSTANCE = null;
    String URL = "";
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;

    private DBConnect() {

    }

    public static DBConnect getInstance() {
        if (INSTANCE == null) {
            synchronized (DBConnect.class) {
                INSTANCE = new DBConnect();
            }
        }
        return INSTANCE;
    }

    public void setConnection(String DBName, String UserName, String Password) {
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            URL = "jdbc:jtds:sqlserver://192.168.0.81:1433;instanceName=SQLEXPRESS;DatabaseName=" + DBName + ";integratedSecurity=true;user=" + UserName + ";password=" + Password;
            conn = DriverManager.getConnection(URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This works on my Genymotion emulator since I also executed a select and seen the results being displayed.

I am using the JTDS driver to do this

However when I try with a real device that is on the same Wi-Fi network it doesn't work, I get a connection error.

Any ideas why I am getting this?

This is my error log:

java.sql.SQLException: Network error IOException: failed to connect to /192.168.0.81 (port 1433) after 90000ms
07-18 08:29:09.465  27413-27514/ordermanager.sentosa.ro.ordermanager W/System.err﹕ at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:410)
07-18 08:29:09.466  27413-27514/ordermanager.sentosa.ro.ordermanager W/System.err﹕ at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
07-18 08:29:09.466  27413-27514/ordermanager.sentosa.ro.ordermanager W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:175)
07-18 08:29:09.467  27413-27514/ordermanager.sentosa.ro.ordermanager W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:140)
07-18 08:29:09.467  27413-27514/ordermanager.sentosa.ro.ordermanager W/System.err﹕ at ordermanager.sentosa.ro.ordermanager.utils.DBConnect.setConnection(DBConnect.java:39)

解决方案

Two possible solutions to your problem that i can see:

  1. your are using a Genymotion emulator which is probably working on a different network
  2. check if your SQL Server database is accessible in online mode by setting this:

    ALTER DATABASE database-name SET ONLINE

这篇关于连接本地SQL Server数据库工作在模拟器,但未能真实设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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