Java 将android连接到mysql数据库
我不明白为什么我的应用程序会崩溃Java 将android连接到mysql数据库,java,android,database,connection,database-connection,Java,Android,Database,Connection,Database Connection,我不明白为什么我的应用程序会崩溃 import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.Menu; import android.view.MenuItem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import java.sql.*;
import java.util.*;
public class MyActivity extends Activity {
EditText e;
Button bt;
ListView lv;
Connection connect;
SimpleAdapter sm;
public void declere() {
e = (EditText) findViewById(R.id.txt_buscar);
bt = (Button) findViewById(R.id.btn_buscar);
lv = (ListView) findViewById(R.id.list_output);
}
public void initilize() {
declere();
e.setText("SELECT * FROM TipoLugar");
connect = CONN("root", "teste", "teste_DB", "192.168.5.154:3306");
}
@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 cl) {
Log.e("ERROR", cl.getMessage());
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
return conn;
}
public void querySQL(String COMMANDSQL) {
ResultSet rs;
try {
Statement statement = connect.createStatement();
rs = statement.executeQuery(COMMANDSQL);
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("code"));
datanum.put("b", rs.getString("descricao"));
data.add(datanum);
}
String[] from = {"a", "b"};
int[] to = {R.id.txt_titulo, R.id.txt_conteudo};
sm = new SimpleAdapter(this, data, R.layout.modelo, from, to);
lv.setAdapter(sm);
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
}
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_my);
initilize();
bt.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
querySQL(e.getText().toString());
}
});
}
07-08 16:51:31.420 632-632/app2.francisco.app2 I/Process﹕ 发送信号。PID:632 SIG:9看起来rs为空,这意味着您设置它的部分不起作用 语句。executeQueryCOMMANDSQL;是null,因此您需要遵循该逻辑并找出导致它等于null的原因,这可能是您的sql语法,因为您将其设置为等于文本字段输入,而且executeQUery和execute是不同的东西,并返回不同的值,我建议查看API以确保您得到了想要的在querySQL方法的catch块中,e.getMessage似乎为null,因此Log.e将null传递给println,println将抛出NullPointerException 为什么异常消息为null,我不能说,但您可以通过检查null并在以下情况下提供默认值来解决此问题:
public void querySQL(String COMMANDSQL) {
...
try {
...
} catch (Exception e) {
String errorMessage = (e.getMessage() != null) ? e.getMessage() : "Unknown error";
Log.e("ERROR", errorMessage);
}
}
这显示了一个错误。字符串errorMessage=e.getMessage!=null e.getMessage:未知错误@用户3816937对不起。。就我而言,输入错误遗漏了三元运算符中最重要的部分。现在修复。它落在那个错误上。。。在SQLEXCEPTION上。。。你知道为什么吗?thx@LimbSoup@user3816937这听起来像是另一个问题,因为您的NullPointerException应该得到修复。如果不知道查询是什么、模式,就不可能找出SqlException的原因。。等等,是我的错。。。。。查询错误,用户也。。。。。。。。。。。。。。。。。。。。很抱歉,谢谢大家
public void querySQL(String COMMANDSQL) {
...
try {
...
} catch (Exception e) {
String errorMessage = (e.getMessage() != null) ? e.getMessage() : "Unknown error";
Log.e("ERROR", errorMessage);
}
}