如何从Android应用程序连接Sql Server数据库?

时间:2012-04-19 09:44:48

标签: android sql-server

我想使用JTDS驱动程序连接sql server。 但它抛出网络错误IOException:操作超时。 任何人都可以告诉我...... 提前致谢...... 这是我的代码......

try
{
    Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/tempdb", "sa", "");
    System.out.println("connected : " + conn);     
}
catch (Exception e)
{
    e.printStackTrace();
}

它显示Logcat上的错误如下:

04-19 15:02:09.605: WARN/System.err(1245): java.sql.SQLException: Network error IOException: The operation timed out
04-19 15:02:09.865: WARN/System.err(1245):     at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:410)
04-19 15:02:09.875: WARN/System.err(1245):     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
04-19 15:02:09.875: WARN/System.err(1245):     at java.sql.DriverManager.getConnection(DriverManager.java:191)
04-19 15:02:09.875: WARN/System.err(1245):     at java.sql.DriverManager.getConnection(DriverManager.java:226)
04-19 15:02:09.875: WARN/System.err(1245):     at com.android.sample.DB.dbConnect(DB.java:18)
04-19 15:02:09.875: WARN/System.err(1245):     at com.android.sample.SampleActivity.connectDb_onClick(SampleActivity.java:20)
04-19 15:02:09.875: WARN/System.err(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 15:02:09.875: WARN/System.err(1245):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 15:02:09.875: WARN/System.err(1245):     at android.view.View$1.onClick(View.java:2067)
04-19 15:02:09.885: WARN/System.err(1245):     at android.view.View.performClick(View.java:2408)
04-19 15:02:09.885: WARN/System.err(1245):     at android.view.View$PerformClick.run(View.java:8816)
04-19 15:02:09.885: WARN/System.err(1245):     at android.os.Handler.handleCallback(Handler.java:587)
04-19 15:02:09.885: WARN/System.err(1245):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-19 15:02:09.895: WARN/System.err(1245):     at android.os.Looper.loop(Looper.java:123)
04-19 15:02:09.895: WARN/System.err(1245):     at android.app.ActivityThread.main(ActivityThread.java:4635)
04-19 15:02:09.895: WARN/System.err(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 15:02:09.895: WARN/System.err(1245):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 15:02:09.895: WARN/System.err(1245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:916)
04-19 15:02:09.895: WARN/System.err(1245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
04-19 15:02:09.895: WARN/System.err(1245):     at dalvik.system.NativeStart.main(Native Method)
04-19 15:02:09.905: WARN/System.err(1245): Caused by: java.net.SocketException: The operation timed out
04-19 15:02:09.915: WARN/System.err(1245):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
04-19 15:02:09.915: WARN/System.err(1245):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
04-19 15:02:09.925: WARN/System.err(1245):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
04-19 15:02:09.925: WARN/System.err(1245):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:219)
04-19 15:02:09.925: WARN/System.err(1245):     at java.net.Socket.startupSocket(Socket.java:781)
04-19 15:02:09.925: WARN/System.err(1245):     at java.net.Socket.tryAllAddresses(Socket.java:194)
04-19 15:02:09.935: WARN/System.err(1245):     at java.net.Socket.<init>(Socket.java:258)
04-19 15:02:09.935: WARN/System.err(1245):     at java.net.Socket.<init>(Socket.java:222)
04-19 15:02:09.935: WARN/System.err(1245):     at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:259)
04-19 15:02:09.935: WARN/System.err(1245):     at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:311)
04-19 15:02:09.935: WARN/System.err(1245):     ... 19 more

0 个答案:

没有答案