2013-07-18 87 views
0

美好的一天!嘗試連接到SQL Server 2005時出現Android應用程序錯誤

我下面這個教程 - 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()); 
     } 
    }); 
} 

}

回答

0

我發現這個問題。整個代碼是正確的,我只需要將「實例」添加到URL中,並在「SQL Server配置管理器」中設置ALLIP = 1433。

感謝您的關注! BR, Makpal

相關問題