2014-04-16 115 views
1

我试图将我的android应用程序连接到mssql数据库。但当我尝试连接时,我遇到了一个异常。我已经在调试模式下检查,并从Class.forName去异常捕获 这里是我的代码。将mssql连接到android应用程序

protected void onStart() { 
    try 
    { 
     // Load the SQLServerDriver class, build the 
     // connection string, and get a connection 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     String connectionUrl = "jdbc:sqlserver://xxx\\xxxxx:1433;" + 
           "database=xxxx;" + 
           "user=xxxx;" + 
           "password=*****"; 
     Connection con = DriverManager.getConnection(connectionUrl); 
     System.out.println("Connected."); 

     // Create and execute an SQL statement that returns some data. 
     String SQL = "SELECT COUNT(*) FROM xxxxxx"; 
     Statement stmt = con.createStatement(); 
     ResultSet rs = stmt.executeQuery(SQL); 
     TextView tv=(TextView) findViewById(R.id.textView1); 
     tv.setText((CharSequence) rs); 


     // Iterate through the data in the result set and display it. 
     while (rs.next()) 
     { 
      System.out.println(rs.getString(1) + " " + rs.getString(2)); 
     } 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    super.onStart(); 
} 

回答

0

这完全不可能,Android受限于带宽限制,因此无法与任何外部数据库进行通信,但您可以做的是创建一个简单的网页(php),以便从MySQL返回所需内容数据库并将其解析为xml文件或JSON,因为它现在更好,那么你可以从你的android应用程序调用这个php页面并获取JSON文件。

这是一个政党成员解释这个非常好:

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

好运:)

0

你不能从Android应用程序连接到MSSQL那样。您应该创建一个Web服务来处理数据库请求。然后,您可以从您的应用程序调用该Web服务。

相关问题