首先。我是 android 应用程序开发环境的新手,我正在尝试使用 jtds-1.3.1.jar 驱动程序通过简单的 android 应用程序连接到 mssqlserver 2008。我在互联网上搜索了很多示例,但未能连接到数据库。
我得到的异常是 Network error IOException: connection time out
我不知道我使用 Eclipse juno IDE 的代码有什么问题。
这是我的代码
package com.example.Testproject1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.TextView;
import java.sql.*;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectTodatabase();
}
public void connectTodatabase()
{
TextView txtView=(TextView)findViewById(R.id.textView2);
String url = "jdbc:jtds:sqlserver://XXX.XXX.X.XXX:1433;DatabaseName=VautomateuShoppi";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String userName = "VShopping_User";
String password = "VShopping_Pass";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
// Establish the connection.
Class.forName(driver);
con = DriverManager.getConnection(url, userName, password);
// Create and execute an SQL statement that returns some data.
String SQL = "select * from SeoMaster";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
txtView.setText(rs.getString(2));
}
}
catch(Exception ex)
{
txtView.setText(ex.getMessage().toString());
}
}
}
最佳答案
添加这些代码
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
关于java - 将 android 应用程序连接到 Microsoft SQL Server 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26052899/