创建完成后点击登录数据库
发布时间:2025-06-24 19:57:11 作者:北方职教升学中心 阅读量:077
💡需要注意的是:这边的mysql可以使用我的,也可以点击链接下载。即允许所有人访问数据库,若不是自己用的测试的话,建议限制ip.
点击查看链接详情,保存外网端口。
Android代码部分
新建MySQLConnections类,用于链接数据库。
数据库和数据表建完后,即可配置Android部分内容。
发布时间:2025-06-24 19:57:11 作者:北方职教升学中心 阅读量:077
💡需要注意的是:这边的mysql可以使用我的,也可以点击链接下载。即允许所有人访问数据库,若不是自己用的测试的话,建议限制ip.
点击查看链接详情,保存外网端口。
新建MySQLConnections类,用于链接数据库。
数据库和数据表建完后,即可配置Android部分内容。
点击数据库管理,即可创建数据库。
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
创建完成后点击登录数据库。
目录
阿里云部分
Adnroid配置部分
Android代码部分
效果
在阿里云中查看已保存的数据
进入阿里云首页,这里选择的是云数据库RDS MySQL版。
测试的话,设置为0.0.0.0/0就可以。
数据库创建完成后,即可建表。
mysql下载
若当版本不适配,会报错:
java.sql.SQLException: Unknown system variable ‘query_cache_size'
此时就需要更换mysql版本即可解决。
将MySQL包下载后,粘贴至lib中。
变成如下图即添加完毕。
点击“云数据库RDS版”
点击实例列表点击实例ID
接下来是设置白名单。注意不是版本越高越好。
购买完成后,点击控制台。设定你的数据库帐号密码。
并添加库。
public class MainActivity extends AppCompatActivity { private TextView t1; //用于显示获取的信息 private Button sendmsg; //按钮 private EditText et_msg;//用户输入的信息 private String user="用户名"; private boolean T=false;//发送标志位 //数据库连接类 private static Connection con = null; private static PreparedStatement stmt = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 t1 = findViewById(R.id.t1); et_msg = findViewById(R.id.msg); sendmsg = findViewById(R.id.button); sendmsg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { T=true; } }); //TODO 启动发送线程,用按钮控制发送标志位T,来进行发送信息【注意:连接数据库必须在线程内,不然会报错】 Threads_sendmsg threads_sendmsg = new Threads_sendmsg(); threads_sendmsg.start(); //TODO 启动获取数据线程,读取数据库里的信息【注意:连接数据库必须在线程内,不然会报错】 Threads_readSQL threads_readSQL = new Threads_readSQL(); threads_readSQL.start(); } class Threads_sendmsg extends Thread { @Override public void run() { while (true){ while (T){ try { con = MySQLConnections.getConnection(); } catch (Exception e) { e.printStackTrace(); } try { String msg =et_msg.getText().toString().trim(); //用户发送的信息 if (msg.isEmpty()){ Looper.prepare(); Toast.makeText(MainActivity.this, "请不要发送空消息", Toast.LENGTH_SHORT).show(); Looper.loop(); T=false; break; } if (msg.length()>199){ Looper.prepare(); Toast.makeText(MainActivity.this, "请不要发送200字符以上的信息", Toast.LENGTH_SHORT).show(); Looper.loop(); T=false; break; } if (con!=null) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show(); } }); String sql = "insert into test(name,msg) values(?,?)"; stmt = con.prepareStatement(sql); // 关闭事务自动提交 ,这一行必须加上 con.setAutoCommit(false); stmt.setString(1,user); stmt.setString(2,msg); stmt.addBatch(); stmt.executeBatch(); con.commit(); } }catch (SQLException e){ System.out.println(e); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"请输入正确的语句",Toast.LENGTH_SHORT).show(); } }); } T=false; } } } } class Threads_readSQL extends Thread { @Override public void run() { while (true){ try { con = MySQLConnections.getConnection(); } catch (Exception e) { e.printStackTrace(); } try { String sql ="Select name,msg from test order by id"; if (con!=null) { stmt = con.prepareStatement(sql); // 关闭事务自动提交 ,这一行必须加上 con.setAutoCommit(false); ResultSet rs = stmt.executeQuery();//创建数据对象 //清空上次发送的信息 t1.setText(""); while (rs.next()) { t1.append(rs.getString(1) + "\n" + rs.getString(2)+ "\n\n"); } con.commit(); rs.close(); stmt.close(); } //2秒更新一次 sleep(2000); }catch (Exception e){ System.out.println(e); runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(MainActivity.this,"请输入正确的语句",Toast.LENGTH_SHORT).show(); } }); } } } }}
xml文件
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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"> <TextView android:id="@+id/t1" android:layout_width="412dp" android:layout_height="424dp" android:layout_weight="10" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.211" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <EditText android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:ems="10" android:hint="请输入内容" android:inputType="textPersonName" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="发送" /> </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
输入:SELECT * FROM '表名'
即可
资源在这里,里面包含代码及mysql包:
https://download.csdn.net/download/shanhe_yuchuan/87610575
如有问题欢迎评论区或私信,有时间我一定第一时间回复。
public class MySQLConnections { private String driver = ""; private String dbURL = ""; private String user = ""; private String password = ""; private static MySQLConnections connection = null; private MySQLConnections() throws Exception { driver = "com.mysql.jdbc.Driver"; dbURL = "jdbc:mysql://复制的外网地址:3306/创建的数据库名"; user = "你设定的数据库账号,不是阿里云账号"; password = "数据库密码"; System.out.println("dbURL:" + dbURL); } public static Connection getConnection() { Connection conn = null; if (connection == null) { try { connection = new MySQLConnections(); } catch (Exception e) { e.printStackTrace(); return null; } } try { Class.forName(connection.driver); conn = DriverManager.getConnection(connection.dbURL, connection.user, connection.password); } catch (Exception e) { e.printStackTrace(); } return conn; }}
MainActivity.java
主要实现点击按键发送一条数据,并每隔两秒获取数据库中数据。