布局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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/bt_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"/>
<TextView
android:id="@+id/tv_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<Button
android:id="@+id/bt_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"/>
<EditText
android:id="@+id/ed_insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要插入的数据"
android:maxLines="2"/>
<Button
android:id="@+id/bt_insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入"/>
<EditText
android:id="@+id/ed_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要更新的数据"
android:maxLines="2"/>
<Button
android:id="@+id/bt_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新数据"/>
</LinearLayout>
mainactivity.java
package com.example.xxx;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity implements onClick {
private Button button, button_delete, button_insert, button_update;
private TextView textView;
private static final int TEST_USER_SELECT = 1;
int i = 0, d = 0, z = 0;
private EditText editText,editText_update;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
String user;
switch (msg.what){
case TEST_USER_SELECT:
Test test = (Test)msg.obj;
user = test.getUser();
System.out.println("******");
System.out.println("******");
System.out.println("user:"+user);
textView.setText(user);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button) findViewById(R.id.bt_send);
textView =(TextView) findViewById(R.id.tv_response);
button_delete =(Button) findViewById(R.id.bt_delete);
button_insert =(Button) findViewById(R.id.bt_insert);
button_update =(Button) findViewById(R.id.bt_update);
editText_update =(EditText) findViewById(R.id.ed_update);
}
@Override
protected void onStart(){
super.onStart();
button.setOnClickListener((View.OnClickListener) this);
button_delete.setOnClickListener((View.OnClickListener) this);
button_insert.setOnClickListener((View.OnClickListener) this);
button_update.setOnClickListener((View.OnClickListener) this);
}
@Override
public void onClick(View view){
switch (view.getId()){
case R.id.bt_send:
if (i<=3){
i++;
}else {
i=1;
}
new Thread(new Runnable() {
@Override
public void run() {
Connection conn = null;
conn =(Connection) DBOpenHelper.getConn();
String sql = "select 姓名 from 学生";
Statement st;
try {
st =(Statement) conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()){
Test test = new Test();
test.setUser(rs.getString(1));
Message msg = new Message();
msg.what = TEST_USER_SELECT;
msg.obj = test;
handler.sendMessage(msg);
}
st.close();
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}).start();
break;
case R.id.bt_delete:
d++;
new Thread(new Runnable() {
@Override
public void run() {
Connection conn = null;
int u = 0;
conn = (Connection)DBOpenHelper.getConn();
String sql = "";
PreparedStatement pst;
try{
pst =(PreparedStatement) conn.prepareStatement(sql);
u = pst.executeUpdate();
pst.close();
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}).start();
break;
case R.id.bt_insert:
new Thread(new Runnable() {
@Override
public void run() {
Connection conn = null;
int u = 0;
conn = (Connection)DBOpenHelper.getConn();
String sql = "";
PreparedStatement pst;
try {
pst = (PreparedStatement)conn.prepareStatement(sql);
pst.setString(1,editText.getText().toString());
u = pst.executeUpdate();
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.bt_update:
z++;
new Thread(new Runnable() {
@Override
public void run() {
Connection conn = null;
int u = 0;
conn = (Connection)DBOpenHelper.getConn();
String sql = "";
PreparedStatement pst;
try {
pst = (PreparedStatement)conn.prepareStatement(sql);
u = pst.executeUpdate();
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
break;
default:
}
}
}
TEST文件
package com.example.xxx;
public class Test {
private int id;
private String user;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getUser(){
return user;
}
public void setUser(String user){
this.user = user;
}
}
DBOpenHelper
package com.example.xxx;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBOpenHelper {
private static String diver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://192.168.0.103:3306/wdnmd";
private static String user = "root";
private static String password = "Q87683232q";
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(diver);
conn =(Connection) DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}