小编典典

将Android应用连接到Microsoft SQL Server 2008

java

首先。我是android应用程序开发环境的新手,我正在尝试使用jtds-1.3.1.jar驱动程序通过简单的android应用程序连接到mssqlserver
2008。我在互联网上搜索了很多示例,但无法连接数据库。
我得到的异常是 网络错误IOException:连接超时
我不知道我使用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());
    }
    }
}

阅读 283

收藏
2020-12-03

共1个答案

小编典典

添加这些代码

StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
2020-12-03