Java Android AsyncTask + mysql connection(Java Android AsyncTask + mysql 连接)
问题描述
我有一个问题,我正在尝试使用 AsyncTask
连接到 MySQL 数据库.我把连接代码放在按钮事件中,但它给了我错误,
I have a problem, I am trying to connect to a MySQL database using AsyncTask
. I put the connection code in the button event but it gives me error,
代码如下:
package com.example.mysqlconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String url = "jdbc:mysql://cool/app";
private static final String user = "user";
private static final String pass = "password";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Connect();
}
});
}
TextView tv = (TextView)this.findViewById(R.id.textView1);
private class Connect extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
// System.out.println("Database connection success");
String result = "Database connection success
";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "
";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "
";
result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "
";
}
tv.setText(result);
}
catch(Exception e) {
e.printStackTrace();
tv.setText(e.toString());
}
return response;
}
@Override
protected void onPostExecute(String result) {
tv.setText(result);
}
}
public void Connect() {
Connect task = new Connect();
task.execute();
}
}
我是 AsyncTask 的新手,有人能帮我找出错误吗?
I'm new to AsyncTask, can someone help me finding the errors?
这是日志猫:
10-23 19:08:41.168: W/dalvikvm(1514): threadid=1: thread exiting with uncaught exception (group=0xb40ca180)
10-23 19:08:41.178: E/AndroidRuntime(1514): FATAL EXCEPTION: main
10-23 19:08:41.178: E/AndroidRuntime(1514): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlconnect/com.example.mysqlconnect.MainActivity}: java.lang.NullPointerException
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.os.Looper.loop(Looper.java:137)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 19:08:41.178: E/AndroidRuntime(1514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 19:08:41.178: E/AndroidRuntime(1514): at dalvik.system.NativeStart.main(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): Caused by: java.lang.NullPointerException
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.Activity.findViewById(Activity.java:1794)
10-23 19:08:41.178: E/AndroidRuntime(1514): at com.example.mysqlconnect.MainActivity.<init>(MainActivity.java:51)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.Class.newInstanceImpl(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): at java.lang.Class.newInstance(Class.java:1319)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-23 19:08:41.178: E/AndroidRuntime(1514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
我在创建时删除了第二个,现在我在 logcat 中收到此错误:
I removed the second on create and now i get this error in logcat:
10-23 19:10:29.129: E/AndroidRuntime(1565): FATAL EXCEPTION: main
10-23 19:10:29.129: E/AndroidRuntime(1565): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlconnect/com.example.mysqlconnect.MainActivity}: java.lang.NullPointerException
推荐答案
你的错误是:TextView tv = (TextView)this.findViewById(R.id.textView1);
因为它是在外面声明的onCreate
.
Your error is: TextView tv = (TextView)this.findViewById(R.id.textView1);
because it is declared outside of onCreate
.
所以这就是你必须做的,在onCreate
上方声明TextView tv;
.然后在 onCreate
中添加 tv = (TextView) findViewById(R.id.textView1);
So this is what you have to do, declare TextView tv;
above onCreate
. Then inside onCreate
you add tv = (TextView) findViewById(R.id.textView1);
并删除以下重复项:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
这篇关于Java Android AsyncTask + mysql 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java Android AsyncTask + mysql 连接
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01