Hallo Liebe Community,
Ich habe in Android Studio ein Programm geschrieben, welches sich mit einer Datenbank im MySQL Server verbindet und mir dann alle Einträge auf dem Android-Gerät anzeigen soll. Das Problem ist, dass sich mein Program nicht mit meiner Datenbank verbinden lässt. Ich habe viel rumprobiert, aber ich bin letztendlich nicht weiter gekommen. Darum würde ich mich auf eine schnelle Rückmeldung freuen.
Hier der MainActivity Java-Code:
Java: MainActivity Java
package com.example.androidmysql2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import static com.example.androidmysql2.R.id.txtData;
public class MainActivity extends AppCompatActivity {
private static final String url = "jdbc:mysql://:3306/mydb";
private static final String user = "test";
private static final String pass = "password";
Button btnFetch,btnClear;
TextView txtData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtData = (TextView) this.findViewById(R.id.txtData);
btnFetch = (Button) findViewById(R.id.btnFetch);
btnClear = (Button) findViewById(R.id.btnClear);
btnFetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ConnectMySql connectMySql = new ConnectMySql();
connectMySql.execute("");
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtData.setText("");
}
});
}
private class ConnectMySql extends AsyncTask<String, Void, String> {
String res = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "Please wait...", Toast.LENGTH_SHORT)
.show();
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = DriverManager.getConnection(url, user, pass);
System.out.println("Databaseection success");
String result = "Database Connection Successful\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select distinct country from tblcountries");
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
result += rs.getString(1).toString() + "\n";
}
res = result;
} catch (Exception e) {
e.printStackTrace();
res = e.toString();
}
return res;
}
@Override
protected void onPostExecute(String result) {
txtData.setText(result);
}
}
}
Alles anzeigen
Hier der MainActivity Xml-Code:
XML: MainActivity Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:text="Android MySQL Application"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Your Data will be Displayed here"
android:textStyle="bold" />
<Button
android:id="@+id/btnFetch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="2dp"
android:background="@color/colorPrimaryDark"
android:minWidth="250dp"
android:text="Fetch Data"
android:textColor="#fff" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="2dp"
android:background="@color/colorAccent"
android:minWidth="250dp"
android:text="Clear"
android:textColor="#fff" />
</LinearLayout>
Alles anzeigen