Android应用程序连接到SQL Server数据库 [英] Connect android app to sql server database

查看:196
本文介绍了Android应用程序连接到SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty关于Java和Android编程新。
我试图连接与Android应用程序安装在Windows笔记本电脑的一个SQL Server数据库。

I'm pretty new on java and android programming. I'm trying to connect an android application to a SQL Server database installed on a Windows laptop.

我找遍了所有的网络和我fonud了很多的解决方案,没有成功。
我的情况是这样的:

I searched in the all network and I fonud a lot of solutions, without success. My situation is the following:


  • 我使用ADT在Windows下编程

  • 我直接在我的Galaxy S3(果冻豆4.1.2)
  • 运行过程中出现的应用程序
  • 应用程序具有只是一个按钮连接到数据库

  • 在code是以下(放在MainActivity.java):

  • I'm programming under Windows using ADT
  • I'm runnig the application directly on my Galaxy S3 (Jelly Bean 4.1.2)
  • The application has just a button "Connect to the database"
  • The code is the following (placed in the MainActivity.java):

public void ConnectToDatabase(View View){
    try {
         // SET CONNECTIONSTRING
         Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
         Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://12.34.56.78:1433/DatabaseName;user=myusername;password=mypassword");

         // CONNECTION OK: SHOW MESSAGE
         Toast.makeText(this, "Connection successful!", Toast.LENGTH_LONG).show();
     }
}


我添加在 JTDS-1.2.5.jar 两次在那些方面:

I added the jtds-1.2.5.jar twice in those ways:


  • 在Package Explorer中,右键单击应用程序名称

  • 构建路径\\添加外部归档。

  • 搜索手动JTDS-1.2.5.jar文件,确认

然后:


  • 复制手动JTDS-1.2.5.jar文件

  • 在Package Explorer中,右键单击库目录

  • 粘贴

在我的笔记本电脑启用了SQL浏览器服务,端口1433是开放的正确和Windows防火墙服务被禁用。我可以从另一台电脑正常连接到我的数据库。

On my laptop the SQL Browser service is enabled, the port 1433 is correctly open and the windows firewall service is disabled. I can connect normally to my database from another PC.

我分享使用Connectify的与我的Andr​​oid设备我的笔记本电脑的互联网连接。从笔记本电脑,我可以ping成功的智能手机。我用另一台笔记本电脑没有问题也试过这种配置。

I'm sharing the internet connection of my notebook using Connectify with my android device. From the laptop I can ping the smartphone with success. I also tried this configuration using another laptop without problems.

当我preSS的连接到数据库按钮,我收到以下错误信息:

When I press the "connect to database" button I receive the following error message:

Intent error: Network error IOException: socked failed: EACCES (Permission denied)

我在网上搜索,我发现这个解决方案。把互联网的权限清单文件:

I searched online and I found this solution. Put the internet permission in the Manifest file:

<uses-permission android:name="android.permission.INTERNET"/>

之后,当我preSS的连接到数据库按钮,该应用程序只是崩溃。这就是LogCat中的输出:

After that, when I press the "connect to database" button the app just crashes. That's the output of the LogCat:

D/dalvikvm(24311): GC_CONCURRENT freed 180K, 6% free 12426K/13127K, paused 12ms+2ms, total 39ms
D/AndroidRuntime(24311): Shutting down VM
W/dalvikvm(24311): threadid=1: thread exiting with uncaught exception (group=0x40f382a0)
E/AndroidRuntime(24311): FATAL EXCEPTION: main
E/AndroidRuntime(24311): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime(24311):    at android.view.View$1.onClick(View.java:3691)
E/AndroidRuntime(24311):    at android.view.View.performClick(View.java:4211)
E/AndroidRuntime(24311):    at android.view.View$PerformClick.run(View.java:17267)
E/AndroidRuntime(24311):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(24311):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(24311):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(24311):    at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(24311):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24311):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(24311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(24311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(24311):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24311): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(24311):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24311):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(24311):    at android.view.View$1.onClick(View.java:3686)
E/AndroidRuntime(24311):    ... 11 more
E/AndroidRuntime(24311): Caused by: java.lang.NullPointerException
E/AndroidRuntime(24311):    at com.example.app1.MainActivity.ConnectToDatabase(MainActivity.java:74)
E/AndroidRuntime(24311):    ... 14 more

有人可以帮助我?

Can someone help me with this?

在此先感谢

修改

我认为JDBC驱动程序是由应用程序加载正确。该应用程序抛出异常时,它会尝试连接到SQL数据库。只要我特林连接到\\ SQLEX $ P $干燥综合征的数据库,我改变了的ConnectionString如下:

I think that the JDBC driver is loaded correctly by the app. The application throw the exception when it tries to connect to the SQL database. As long as I'm tring to connect to a \SQLEXPRESS database, I changed the connectionstring as follow:

Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://12.34.56.78:1433/DatabaseName;user=myusername;password=mypassword;instance=SQLEXPRESS")

如果我离开istance = SQLEX $ P $干燥综合征的应用程序会引发以下的的SQLException

If I leave the "istance=SQLEXPRESS" the app throw the following SQLException:

"Unable to get information from SQL Server: 12.34.56.78."

如果我脱下istance从我的ConnectionString收到errori如下:

If I take off the istance from the connectionstring the errori I receive is the following:

Error: null

任何想法? (

推荐答案

这可能不相关(因为我从来没有尝试过/听说过直接从Android应用程序访问SQL数据库),但你为什么不创建一个Web服务数据库?由于你的数据库位于你的主机上没有你的手机内存卡上,最终将需要创建一个Web服务,使您的应用程序可以访问数据库。

This may not be relevant (since i never tried/heard to access a sql database directly from android app) but why don't you create a web service for database? Since your database sits on your host not on your phone memory card, eventually you will need to create a web service so that your app can access the database.

这篇关于Android应用程序连接到SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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