【发布时间】:2014-07-08 15:34:03
【问题描述】:
我不明白为什么我的应用程序崩溃了......
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());
}
});
}
log cat 中的错误是这样的(你知道这是什么吗?:))
07-08 16:51:29.200 632-632/app2.francisco.app2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:231)
at app2.francisco.app2.MyActivity.querySQL(MyActivity.java:88)
at app2.francisco.app2.MyActivity$1.onClick(MyActivity.java:103)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
07-08 16:51:31.420 632-632/app2.francisco.app2 I/Process:发送信号。 PID:632 SIG:9
【问题讨论】:
标签: java android database connection database-connection