java-使用JDBC将Android连接到SQL Server

java-使用JDBC将Android连接到SQL Server,第1张

概述我目前正在为Android开发一个应将其数据同步到MSSQLServer2008的应用程序.由于我以前从未做过此事,因此我目前正在测试使其正常工作的方法.我应该提到的是,只要公司不希望在网络上注册设备,只要它连接到USB端口而不是通过WiFi即可同步.到目前为止,这是我已经完成的将Java连接到S

我目前正在为Android开发一个应将其数据同步到MSsql Server 2008的应用程序.由于我以前从未做过此事,因此我目前正在测试使其正常工作的方法.我应该提到的是,只要公司不希望在网络上注册设备,只要它连接到USB端口而不是通过WiFi即可同步.

到目前为止,这是我已经完成的将Java连接到sql Server的工作.这是一个简单的Select代码(我目前正在使用sqlExpress进行测试):

  String connectionUrl = "jdbc:sqlserver://127.0.0.1:1433;" +             "databasename=AndroID;integratedSecurity=true;";  // Declare the JDBC objects.  Connection con = null;  Statement stmt = null;  ResultSet rs = null;  try {     // Establish the connection.     Class.forname("com.microsoft.sqlserver.jdbc.sqlServerDriver");     con = DriverManager.getConnection(connectionUrl);     // Create and execute an sql statement that returns some data.     String sql = "SELECT * FROM AndroIDTest;";     stmt = con.createStatement();     rs = stmt.executequery(sql);     // Iterate through the data in the result set and display it.     while (rs.next()) {        System.out.println(rs.getString(1) + " " + rs.getString(2));     }  }  // Handle any errors that may have occurred.  catch (Exception e) {     e.printstacktrace();  }  finally {     if (rs != null) try { rs.close(); } catch(Exception e) {}     if (stmt != null) try { stmt.close(); } catch(Exception e) {}     if (con != null) try { con.close(); } catch(Exception e) {}  }

现在,我在AndroID中尝试了相同的 *** 作,结果如下所示:

package com.example.testsqlserver;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.Widget.EditText;public class MainActivity extends Activity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }    public voID clickSend(VIEw vIEw) {        (new Thread(new TestThread())).start();    }    public class TestThread extends Thread {      public voID run() {          String connectionUrl = "jdbc:sqlserver://127.0.0.1:1433;" +                     "databasename=AndroID;integratedSecurity=true;";          // Declare the JDBC objects.          Connection con = null;          Statement stmt = null;          try {             // Establish the connection.             Class.forname("com.microsoft.sqlserver.jdbc.sqlServerDriver");             con = DriverManager.getConnection(connectionUrl);             //Get information from EditText             EditText txtTest = (EditText)findVIEwByID(R.ID.txtTest);             EditText txtname = (EditText)findVIEwByID(R.ID.txtname);             String test = txtTest.getText().toString();             String name = txtname.getText().toString();             // Create and execute an sql statement that returns some data.             String sql = "INSERT INTO AndroIDTest VALUES('" + test + "', '" + name + "');";             stmt = con.createStatement();             stmt.executeUpdate(sql);             Log.e("Success", "Success");          }          // Handle any errors that may have occurred.          catch (Exception e) {             e.printstacktrace();              Log.e("Error", e.toString());          }          finally {             if (stmt != null) try { stmt.close(); } catch(Exception e) {}             if (con != null) try { con.close(); } catch(Exception e) {}          }      }      public voID main(String args[]) {          (new TestThread()).start();      }    }}

在第一个示例中,它可以正常工作,但是在第二个示例中,它给了我这个错误:

12-17 20:15:12.589: E/Error(1668):
com.microsoft.sqlserver.jdbc.sqlServerException: The TCP/IP connection
to the host 127.0.0.1, port 1433 has Failed. Error: “Failed to connect
to /127.0.0.1 (port 1433) after 403ms: isConnected Failed:
ECONNREFUSED (Connection refused). Verify the connection propertIEs,
check that an instance of sql Server is running on the host and
accepting TCP/IP connections at the port, and that no firewall is
blocking TCP connections to the port.”.

我第一次运行第一个代码时遇到了该错误,我只需要在sql Server设置中启用端口1433.不过,我不明白为什么它不能在第二张桌子上工作.这是相同的代码,唯一的区别是它是通过按按钮执行的,并且我在单独的线程上运行它.

任何帮助将不胜感激,谢谢.

解决方法:

请参阅Emulator Netorking的本节.

您需要使用10.0.2.2,它允许您从仿真器通信到开发机器127.0.0.1地址.

您可能还必须进行一些端口重定向(请参阅该文档中的更多内容).

总结

以上是内存溢出为你收集整理的java-使用JDBC将Android连接到SQL Server全部内容,希望文章能够帮你解决java-使用JDBC将Android连接到SQL Server所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1086161.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存