美好的一天!
我正在关注本教程 - 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 :(得分:0)
我发现了问题。整个代码是正确的,我只需要在URL中添加“instance”并在“SQL Server Configuration Manager”中设置ALLIP = 1433.
感谢您的关注! BR, Makpal