我目前正在爲Android創建一個應用程序,該應用程序應該將其數據同步到MSSQL Server 2008中。我目前正在測試使其工作的方式,因爲我之前從未做過它。我應該提到,只要設備連接到USB端口,而不是通過WiFi,設備就會同步,因爲公司不希望在網絡上註冊設備。使用JDBC將Android連接到SQL Server
到目前爲止,我已經制定出將Java連接到SQL Server。這是一個簡單的選擇代碼(我目前使用的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.".
我有這樣的錯誤,我第一次遇到的第一個代碼,我只是不得不啓用端口1433的SQL Server設置。但我不明白爲什麼它不在第二張桌子上工作。它是相同的代碼,唯一的區別是它是通過按下按鈕執行的,並且我已經將它在單獨的線程上運行。
任何幫助將不勝感激,謝謝。
+1。這個答案比我的答案有更多的信息。 – kosa