0

再会!

我正在关注本教程 - http://www.youtube.com/watch?v=eiSMgy9UciI

并尝试将我的 Android 应用程序连接到 SQL Server 2005。应用程序应该从数据库中获取数据并将其显示在模拟器中。现在,我遇到了未知错误,在运行应用程序时它显示空屏幕和消息“”,并且控制台或 LogCat 中没有发生错误。

这是我的 first_activity.java 代码:

public class First_activity extends Activity {

// creating the objects
Button EXECUTAR;
EditText ValorBusca;
ListView Lista;
Connection connect;
SimpleAdapter AD;

//declaring the objects
    private void declarar()
    {
        EXECUTAR = (Button) findViewById(R.id.btn_buscar);
        ValorBusca = (EditText) findViewById(R.id.txt_buscar);
        Lista = (ListView) findViewById(R.id.list_output);
    }

//initialize objects 
private void inicializar()
{       
    declarar();
ValorBusca.setText("Select ID_pl, pl_name from Planogram_HEAD");
connect = CONN("sa", "Zxc123456", "Market", "10.0.2.2:1433");
}

//create classes
@SuppressLint("NewApi")
private Connection CONN (String _user, String _pass, String _DB, String _server)
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnUrl = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnUrl = "jdbc:jtds:sqlserver://"+ _server + ";" + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
        conn = DriverManager.getConnection(ConnUrl);

    } catch (SQLException se) {
        Log.e("ERROR", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERROR", e.getMessage());
    } catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }
    System.out.println("connected");
    return conn;
}


public void QuerySQL (String COMANDOSQL) {
    ResultSet rs;
    try {
        Statement statement = connect.createStatement();
        rs = statement.executeQuery(COMANDOSQL);
        //configuring of simple Adapter

        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String, String>>();

        while(rs.next()) {
            Map<String, String> datanum = new HashMap<String, String>();
            datanum.put("A", rs.getString("ID_pl"));
            datanum.put("B", rs.getString("pl_name"));
            data.add(datanum);
        }

        String[] from = {"A", "B"};
        int[] views = {R.id.tex_title, R.id.text_content};
        AD = new SimpleAdapter(this, data, R.layout.model, from, views);
        Lista.setAdapter(AD);
} catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_activity);

    inicializar();
    EXECUTAR.setOnClickListener(new OnClickListener () {
        public void onClick(View v){
            QuerySQL(ValorBusca.getText().toString());
        }
    });
}

}

4

1 回答 1

0

我发现了问题。整个代码是正确的,我只需要在 URL 中添加“实例”并在“SQL Server 配置管理器”中设置 ALLIP = 1433。

感谢关注!BR,马克帕尔

于 2013-07-23T03:10:25.210 回答