Android Studio通过SpringBoot连接MySQL数据库实现增删查改
文章目录
前言
Android通过jdbc连接MySQL只可以连接MySQL5.1.x,不可以连接MySQL8.0.x
也不推荐使用Android应用程序中的JDBC,它是不安全的,并且通常无法直接连接到数据库。
springboot连接mysql
后端可以参考
SpringBoot连接MySQL获取数据写后端接口
Android Studio通过okhttp3网络请求实现增删查改
引入依赖
implementation "com.squareup.okhttp3:okhttp:4.9.2"
activity_main
<?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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="as访问MySQL测试"
android:textColor="#03A9F4"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询" />
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id"
android:textColor="#F44336"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#F44336"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number"
android:textColor="#F44336"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="author"
android:textColor="#F44336"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加" />
<EditText
android:id="@+id/et_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/et_add_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="author"
android:inputType="textPersonName" />
<EditText
android:id="@+id/et_add_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="number"
android:inputType="textPersonName" />
<Button
android:id="@+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除" />
<EditText
android:id="@+id/et_delete_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="name"
android:inputType="textPersonName" />
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改" />
<EditText
android:id="@+id/et_update_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="id"
android:inputType="textPersonName" />
<EditText
android:id="@+id/et_update_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/et_update_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="author"
android:inputType="textPersonName" />
<EditText
android:id="@+id/et_update_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="number"
android:inputType="textPersonName" />
</LinearLayout>
MainActivity
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_count = findViewById(R.id.btn_count);
btn_add = findViewById(R.id.btn_add);
btn_delete = findViewById(R.id.btn_delete);
btn_update = findViewById(R.id.btn_update);
tv_name = findViewById(R.id.tv_name);
tv_id = findViewById(R.id.tv_id);
tv_number = findViewById(R.id.tv_number);
tv_author = findViewById(R.id.tv_author);
et_add_name = findViewById(R.id.et_add_name);
et_add_author = findViewById(R.id.et_add_author);
et_add_number = findViewById(R.id.et_add_number);
et_delete_name = findViewById(R.id.et_delete_name);
et_update_id = findViewById(R.id.et_update_id);
et_update_name = findViewById( R.id.et_update_name);
et_update_author = findViewById(R.id.et_update_author);
et_update_number = findViewById(R.id.et_update_number);
btn_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getBookList();
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addBook(et_add_name.getText().toString(),et_add_author.getText().toString(),et_add_number.getText().toString());
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteBook(et_delete_name.getText().toString());
}
});
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateBook(et_update_id.getText().toString(),et_update_name.getText().toString(),et_update_author.getText().toString(),et_update_number.getText().toString());
}
});
}
查询数据
public void getBookList(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String name = null,id = null,number = null,author = null;
OkHttpClient client =new OkHttpClient();
FormBody.Builder requestBuild=new FormBody.Builder();
Request request=new Request.Builder()
.url("http://10.138.121.198:8080/springboot/getbookList") // url
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
for(int i=0;i<result_json.length();i++) {
id=result_json.getJSONObject(i).getString("id");
name=result_json.getJSONObject(i).getString("name");
number=result_json.getJSONObject(i).getString("number");
author=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = name;
String finalId = id;
String finalNumber = number;
String finalAuthor = author;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException e) {
e.printStackTrace();
}catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
添加数据
public void addBook(String name,String author,String number){
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json =new JSONObject();
json.put("addName" , name);
json.put("addAuthor" , author);
json.put("addNumber" , number);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
Log.i("test",name+" "+author+" "+number);
Request request = new Request.Builder()
.url("http://10.138.121.198:8080/springboot/addBook") // url
.post(requestBody)
.header("contentType","application/json;charset=utf-8")
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
String getName=null,getId=null,getNumber=null,getAuthor=null;
for(int i=0;i<result_json.length();i++) {
getName=result_json.getJSONObject(i).getString("id");
getId=result_json.getJSONObject(i).getString("name");
getNumber=result_json.getJSONObject(i).getString("number");
getAuthor=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = getName;
String finalId = getId;
String finalNumber = getNumber;
String finalAuthor = getAuthor;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException e) {
e.printStackTrace();
}catch (JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
删除数据
public void deleteBook(String name){
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json =new JSONObject();
json.put("deleteName", name);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
Request request = new Request.Builder()
.url("http://10.138.121.198:8080/springboot/deleteBook") // url
.post(requestBody)
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
String getName=null,getId=null,getNumber=null,getAuthor=null;
for(int i=0;i<result_json.length();i++) {
getName=result_json.getJSONObject(i).getString("id");
getId=result_json.getJSONObject(i).getString("name");
getNumber=result_json.getJSONObject(i).getString("number");
getAuthor=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = getName;
String finalId = getId;
String finalNumber = getNumber;
String finalAuthor = getAuthor;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
修改数据
public void updateBook(String id ,String name,String author,String number){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json =new JSONObject();
json.put("updateName" , name);
json.put("updateAuthor" , author);
json.put("updateNumber" , number);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
Request request = new Request.Builder()
.url("http://10.138.121.198:8080/springboot/updateBook") // url
.post(requestBody)
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
String getName=null,getId=null,getNumber=null,getAuthor=null;
for(int i=0;i<result_json.length();i++) {
getName=result_json.getJSONObject(i).getString("id");
getId=result_json.getJSONObject(i).getString("name");
getNumber=result_json.getJSONObject(i).getString("number");
getAuthor=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = getName;
String finalId = getId;
String finalNumber = getNumber;
String finalAuthor = getAuthor;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
完整代码
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
Button btn_count;
Button btn_add;
Button btn_delete;
Button btn_update;
TextView tv_name;
TextView tv_id;
TextView tv_number;
TextView tv_author;
EditText et_add_name;
EditText et_add_author;
EditText et_add_number;
EditText et_delete_name;
EditText et_update_id;
EditText et_update_name;
EditText et_update_author;
EditText et_update_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_count = findViewById(R.id.btn_count);
btn_add = findViewById(R.id.btn_add);
btn_delete = findViewById(R.id.btn_delete);
btn_update = findViewById(R.id.btn_update);
tv_name = findViewById(R.id.tv_name);
tv_id = findViewById(R.id.tv_id);
tv_number = findViewById(R.id.tv_number);
tv_author = findViewById(R.id.tv_author);
et_add_name = findViewById(R.id.et_add_name);
et_add_author = findViewById(R.id.et_add_author);
et_add_number = findViewById(R.id.et_add_number);
et_delete_name = findViewById(R.id.et_delete_name);
et_update_id = findViewById(R.id.et_update_id);
et_update_name = findViewById( R.id.et_update_name);
et_update_author = findViewById(R.id.et_update_author);
et_update_number = findViewById(R.id.et_update_number);
btn_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getBookList();
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addBook(et_add_name.getText().toString(),et_add_author.getText().toString(),et_add_number.getText().toString());
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteBook(et_delete_name.getText().toString());
}
});
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateBook(et_update_id.getText().toString(),et_update_name.getText().toString(),et_update_author.getText().toString(),et_update_number.getText().toString());
}
});
}
//查询数据
public void getBookList(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String name = null,id = null,number = null,author = null;
OkHttpClient client =new OkHttpClient();
FormBody.Builder requestBuild=new FormBody.Builder();
Request request=new Request.Builder()
.url("http://10.138.121.198:8080/springboot/getbookList") // url
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
for(int i=0;i<result_json.length();i++) {
id=result_json.getJSONObject(i).getString("id");
name=result_json.getJSONObject(i).getString("name");
number=result_json.getJSONObject(i).getString("number");
author=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = name;
String finalId = id;
String finalNumber = number;
String finalAuthor = author;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException e) {
e.printStackTrace();
}catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
//添加数据
public void addBook(String name,String author,String number){
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json =new JSONObject();
json.put("addName" , name);
json.put("addAuthor" , author);
json.put("addNumber" , number);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
Log.i("test",name+" "+author+" "+number);
Request request = new Request.Builder()
.url("http://10.138.121.198:8080/springboot/addBook") // url
.post(requestBody)
.header("contentType","application/json;charset=utf-8")
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
String getName=null,getId=null,getNumber=null,getAuthor=null;
for(int i=0;i<result_json.length();i++) {
getName=result_json.getJSONObject(i).getString("id");
getId=result_json.getJSONObject(i).getString("name");
getNumber=result_json.getJSONObject(i).getString("number");
getAuthor=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = getName;
String finalId = getId;
String finalNumber = getNumber;
String finalAuthor = getAuthor;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException e) {
e.printStackTrace();
}catch (JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
//删除数据
public void deleteBook(String name){
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json =new JSONObject();
json.put("deleteName", name);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
Request request = new Request.Builder()
.url("http://10.138.121.198:8080/springboot/deleteBook") // url
.post(requestBody)
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
String getName=null,getId=null,getNumber=null,getAuthor=null;
for(int i=0;i<result_json.length();i++) {
getName=result_json.getJSONObject(i).getString("id");
getId=result_json.getJSONObject(i).getString("name");
getNumber=result_json.getJSONObject(i).getString("number");
getAuthor=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = getName;
String finalId = getId;
String finalNumber = getNumber;
String finalAuthor = getAuthor;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
//修改数据
public void updateBook(String id ,String name,String author,String number){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json =new JSONObject();
json.put("updateName" , name);
json.put("updateAuthor" , author);
json.put("updateNumber" , number);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
Request request = new Request.Builder()
.url("http://10.138.121.198:8080/springboot/updateBook") // url
.post(requestBody)
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
Log.i("test", result);
JSONArray result_json=new JSONArray(result);
String getName=null,getId=null,getNumber=null,getAuthor=null;
for(int i=0;i<result_json.length();i++) {
getName=result_json.getJSONObject(i).getString("id");
getId=result_json.getJSONObject(i).getString("name");
getNumber=result_json.getJSONObject(i).getString("number");
getAuthor=result_json.getJSONObject(i).getString("author");
}
Handler mainHandler = new Handler(Looper.getMainLooper());
String finalName = getName;
String finalId = getId;
String finalNumber = getNumber;
String finalAuthor = getAuthor;
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
tv_name.setText(finalName);
tv_author.setText(finalAuthor);
tv_number.setText(finalNumber);
tv_id.setText(finalId);
}
});
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
最后
如果不懂可以私聊我哟。