Android Studio和阿里云數(shù)據(jù)庫實(shí)現(xiàn)一個(gè)遠(yuǎn)程聊天程序
沒有阿里云數(shù)據(jù)庫的可以買個(gè)最便宜的,我是新用戶9.9元買了一個(gè)
1.買到后點(diǎn)擊左上角的工作臺(tái)

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

開始寫Android Studio項(xiàng)目代碼了,先來看看我的項(xiàng)目結(jié)構(gòu)

依賴包下載地址 Central Repository: mysql/mysql-connector-java (maven.org)
我第一次下了個(gè)版本比較新的發(fā)現(xiàn)會(huì)報(bào)錯(cuò),由于我能力有限,所以就老實(shí)下載一個(gè)低版本的
添加依賴包應(yīng)該都會(huì)了吧,不要忘了添加后還要添加到模塊

MainActivity代碼如下
注意代碼里涉及SQL語句,這里要根據(jù)你之前新建的數(shù)據(jù)庫和新建的表來寫,我新建的表是test
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private TextView t1; //用于顯示獲取的信息
private Button sendmsg; //發(fā)送消息按鈕
private EditText et_msg;//用戶輸入信息框
private String user="user"; //默認(rèn)用戶昵稱
private boolean T=false;//發(fā)送標(biāo)志位
//數(shù)據(jù)庫連接類對(duì)象
private static Connection con = null;
private static PreparedStatement stmt = null;
private Button revise;
private EditText et_revise;
@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);
et_revise = findViewById(R.id.reviseText);
sendmsg = findViewById(R.id.button);
revise = findViewById(R.id.revise);
revise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
user = et_revise.getText().toString();
Toast.makeText(MainActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
}
});
sendmsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { T=true; }
});
//TODO 啟動(dòng)發(fā)送線程,用按鈕控制發(fā)送標(biāo)志位T,來進(jìn)行發(fā)送信息【注意:連接數(shù)據(jù)庫必須在線程內(nèi),不然會(huì)報(bào)錯(cuò)】
Threads_sendmsg threads_sendmsg = new Threads_sendmsg();
threads_sendmsg.start();
//TODO 啟動(dòng)獲取數(shù)據(jù)線程,讀取數(shù)據(jù)庫里的信息【注意:連接數(shù)據(jù)庫必須在線程內(nèi),不然會(huì)報(bào)錯(cuò)】
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 {
//注意你數(shù)據(jù)庫中是否有 test 這個(gè)表,我新建的表是 test
//還有我的屬性,是否和我一樣呢,不一樣就按你自己的來吧
String msg =et_msg.getText().toString().trim(); //用戶發(fā)送的信息
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, "消息長度超過限制", Toast.LENGTH_SHORT).show();
Looper.loop();
T=false;
break;
}
if (con!=null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "發(fā)送成功", Toast.LENGTH_SHORT).show();
}
});
String sql = "insert into test(name,msg,sign) values(?,?,?)";
stmt = con.prepareStatement(sql);
// 關(guān)閉事務(wù)自動(dòng)提交 ,這一行必須加上
con.setAutoCommit(false);
stmt.setString(1,user);
stmt.setString(2,msg);
stmt.setInt(3,1);
stmt.addBatch();
stmt.executeBatch();
con.commit();
}
}catch (SQLException e){
System.out.println(e);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"請(qǐng)輸入正確的語句",Toast.LENGTH_SHORT).show();
}
});
}
T=false;
}
}
}
}
class Threads_readSQL extends Thread {
ResultSet rs;
@Override
public void run() {
while (true) {
try {
con = MySQLConnections.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
try {
//注意你數(shù)據(jù)庫中是否有 test 這個(gè)表,我新建的表是 test
//還有我的屬性,是否和我一樣呢,不一樣就按你自己的來吧
String sql = "select name,msg,sign from test";
if (con != null) {
stmt = con.prepareStatement(sql);
// 關(guān)閉事務(wù)自動(dòng)提交
con.setAutoCommit(false);
rs = stmt.executeQuery();//創(chuàng)建數(shù)據(jù)對(duì)象
//清空上次發(fā)送的信息
t1.setText(null);
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,"請(qǐng)輸入正確的語句",Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
}
MainActivity布局文件activity_main.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">
// An highlighted block
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:text="Hello World!"
android:layout_marginLeft="8dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/reviseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="請(qǐng)輸入你的昵稱"
android:inputType="textPersonName" />
<Button
android:id="@+id/revise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="修改" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:hint="請(qǐng)輸入內(nèi)容"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="發(fā)送" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MYSQLConnections代碼如下
注意我寫的注釋,用戶名,密碼,外網(wǎng)地址,外網(wǎng)端口號(hào),數(shù)據(jù)庫名稱,這些都要填寫你自己的
import java.sql.Connection;
import java.sql.DriverManager;
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"; //這里根據(jù)你下載的依賴包版本會(huì)有不同的寫法,下載低版本的就是這樣寫
//rm-bp1lxt0mjpf6o.mysql.rds.aliyuncs.com:3306 這個(gè)是外網(wǎng)地址,3306是外網(wǎng)端口號(hào),這些都需要填寫你自己的
//sqlconsole 這個(gè)是你登錄你的數(shù)據(jù)庫后新建的數(shù)據(jù)庫(應(yīng)該不繞口吧)
dbURL = "jdbc:mysql://rm-bp1lxt0m.mysql.rds.aliyuncs.com:3306/sqlconsole";
user = "user"; //你新建庫時(shí)的用戶名
password = "123456"; //你新建庫時(shí)的密碼,這里我就不寫我的真密碼了
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;
}
}
AndroidManifest.xml
注意這里要添加網(wǎng)絡(luò)請(qǐng)求權(quán)限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysqlconnections">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication1">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最后看我運(yùn)行結(jié)果
參考博客
Android Studio 連接阿里云數(shù)據(jù)庫【制作基于數(shù)據(jù)庫的多人遠(yuǎn)程聊天APP】
到此這篇關(guān)于Android Studio和阿里云數(shù)據(jù)庫實(shí)現(xiàn)一個(gè)遠(yuǎn)程聊天程序的文章就介紹到這了,更多相關(guān)Android Studio阿里云遠(yuǎn)程聊天內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- android Socket實(shí)現(xiàn)簡單聊天功能以及文件傳輸
- Android高仿微信聊天界面代碼分享
- android 仿微信聊天氣泡效果實(shí)現(xiàn)思路
- Android 應(yīng)用APP加入聊天功能
- Android如何獲取QQ與微信的聊天記錄并保存到數(shù)據(jù)庫詳解
- 詳解Android 獲取手機(jī)中微信聊天記錄方法
- Android藍(lán)牙通信聊天實(shí)現(xiàn)發(fā)送和接受功能
- Android中基于XMPP協(xié)議實(shí)現(xiàn)IM聊天程序與多人聊天室
- Android實(shí)現(xiàn)聊天界面
- Android?Studio實(shí)現(xiàn)智能聊天
相關(guān)文章
Android 使用mediaplayer播放res/raw文件夾中的音樂的實(shí)例
這篇文章主要介紹了Android 使用mediaplayer播放res/raw文件夾中的音樂的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android編程中activity的完整生命周期實(shí)例詳解
這篇文章主要介紹了Android編程中activity的完整生命周期,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android編程中activity的原理與具體用法,需要的朋友可以參考下2015-12-12
android?viewflipper實(shí)現(xiàn)左右滑動(dòng)切換顯示圖片
這篇文章主要為大家詳細(xì)介紹了android?viewflipper實(shí)現(xiàn)左右滑動(dòng)切換顯示圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android UI自定義ListView實(shí)現(xiàn)下拉刷新和加載更多效果
這篇文章主要介紹了Android UI自定義ListView實(shí)現(xiàn)下拉刷新和加載更多效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法,簡單分析了上下文菜單的功能及ListView創(chuàng)建上下文菜單(ContextMenu)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果
這篇文章主要為大家介紹了Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
這篇文章主要介紹了Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Android編程開發(fā)之多點(diǎn)觸摸(Multitouch)實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程開發(fā)之多點(diǎn)觸摸(Multitouch)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android多點(diǎn)觸摸的相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下2016-08-08
一文帶你搞清楚Android游戲發(fā)行切包資源ID那點(diǎn)事
這篇文章主要介紹了Android 解決游戲發(fā)行切包資源ID的一些問題,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2023-05-05

