将Android应用程序连接到Microsoft SQL Server 2008

时间:2014-09-26 05:52:36

标签: java android sql-server eclipse jtds

首先。我是Android应用程序开发环境的新手,我正在尝试使用简单的android应用程序使用jtds-1.3.1.jar驱动程序连接到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());
    }
    }
}

2 个答案:

答案 0 :(得分:2)

添加这些代码

StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

答案 1 :(得分:1)

试试这个

public List<String> dbConnect(String Host, String Port, String db_userid,
                String db_password) {
            List<String> Db_list = new ArrayList<String>();
            try {
                String ConnectionString = "jdbc:jtds:sqlserver://" + Host + ":"
                        + Port;
                // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection(ConnectionString,
                        db_userid, db_password);
                System.out.println("connected");
                Statement statement = conn.createStatement();
                String queryString = "select name from sys.databases";
                ResultSet rs = statement.executeQuery(queryString);
                while (rs.next()) {
                    Db_list.add(rs.getString(1));
                }
            } catch (Exception e) {
                Db_list.add("Error");
                e.printStackTrace();
            }
            return Db_list;
        }

参考Sql Database connect Using the JDBC Driver with android