国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

這篇具有很好參考價(jià)值的文章主要介紹了Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

目錄

阿里云部分

Adnroid配置部分

Android代碼部分

效果

在阿里云中查看已保存的數(shù)據(jù)


阿里云部分

進(jìn)入阿里云首頁(yè),這里選擇的是云數(shù)據(jù)庫(kù)RDS?MySQL版。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

購(gòu)買(mǎi)完成后,點(diǎn)擊控制臺(tái)。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

點(diǎn)擊“云數(shù)據(jù)庫(kù)RDS版”

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

點(diǎn)擊實(shí)例列表點(diǎn)擊實(shí)例ID

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

接下來(lái)是設(shè)置白名單。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

測(cè)試的話(huà),設(shè)置為0.0.0.0/0就可以。即允許所有人訪(fǎng)問(wèn)數(shù)據(jù)庫(kù),若不是自己用的測(cè)試的話(huà),建議限制ip.

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

點(diǎn)擊查看鏈接詳情,保存外網(wǎng)端口。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

點(diǎn)擊數(shù)據(jù)庫(kù)管理,即可創(chuàng)建數(shù)據(jù)庫(kù)。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

創(chuàng)建完成后點(diǎn)擊登錄數(shù)據(jù)庫(kù)。設(shè)定你的數(shù)據(jù)庫(kù)帳號(hào)密碼。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

數(shù)據(jù)庫(kù)創(chuàng)建完成后,即可建表。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

數(shù)據(jù)庫(kù)和數(shù)據(jù)表建完后,即可配置Android部分內(nèi)容。

Adnroid配置部分

  • Manifest中添加如下權(quán)限,用于訪(fǎng)問(wèn)網(wǎng)絡(luò):
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 導(dǎo)入MySQL包。

將MySQL包下載后,粘貼至lib中。

??需要注意的是:這邊的mysql可以使用我的,也可以點(diǎn)擊鏈接下載。注意不是版本越高越好。

mysql下載

若當(dāng)版本不適配,會(huì)報(bào)錯(cuò):

java.sql.SQLException: Unknown system variable ‘query_cache_size'?

此時(shí)就需要更換mysql版本即可解決。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

并添加庫(kù)。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

變成如下圖即添加完畢。

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

Android代碼部分

新建MySQLConnections類(lèi),用于鏈接數(shù)據(jù)庫(kù)。

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://復(fù)制的外網(wǎng)地址:3306/創(chuàng)建的數(shù)據(jù)庫(kù)名";
        user = "你設(shè)定的數(shù)據(jù)庫(kù)賬號(hào),不是阿里云賬號(hào)";
        password = "數(shù)據(jù)庫(kù)密碼";
        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

主要實(shí)現(xiàn)點(diǎn)擊按鍵發(fā)送一條數(shù)據(jù),并每隔兩秒獲取數(shù)據(jù)庫(kù)中數(shù)據(jù)。

public class MainActivity extends AppCompatActivity {
    private TextView t1;  //用于顯示獲取的信息
    private Button sendmsg;  //按鈕
    private EditText et_msg;//用戶(hù)輸入的信息
    private String user="用戶(hù)名";
    private boolean T=false;//發(fā)送標(biāo)志位
    //數(shù)據(jù)庫(kù)連接類(lèi)
    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 啟動(dòng)發(fā)送線(xiàn)程,用按鈕控制發(fā)送標(biāo)志位T,來(lái)進(jìn)行發(fā)送信息【注意:連接數(shù)據(jù)庫(kù)必須在線(xiàn)程內(nèi),不然會(huì)報(bào)錯(cuò)】
        Threads_sendmsg threads_sendmsg = new Threads_sendmsg();
        threads_sendmsg.start();
        //TODO 啟動(dòng)獲取數(shù)據(jù)線(xiàn)程,讀取數(shù)據(jù)庫(kù)里的信息【注意:連接數(shù)據(jù)庫(kù)必須在線(xiàn)程內(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 {
                        String msg =et_msg.getText().toString().trim();    //用戶(hù)發(fā)送的信息

                        if (msg.isEmpty()){

                            Looper.prepare();
                            Toast.makeText(MainActivity.this, "請(qǐng)不要發(fā)送空消息", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                            T=false;
                            break;
                        }
                        if (msg.length()>199){
                            Looper.prepare();
                            Toast.makeText(MainActivity.this, "請(qǐng)不要發(fā)送200字符以上的信息", 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)  values(?,?)";
                            stmt = con.prepareStatement(sql);
                            // 關(guān)閉事務(wù)自動(dòng)提交 ,這一行必須加上
                            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,"請(qǐng)輸入正確的語(yǔ)句",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);
                        // 關(guān)閉事務(wù)自動(dòng)提交 ,這一行必須加上
                        con.setAutoCommit(false);
                        ResultSet rs = stmt.executeQuery();//創(chuàng)建數(shù)據(jù)對(duì)象
                        //清空上次發(fā)送的信息
                        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,"請(qǐng)輸入正確的語(yǔ)句",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="請(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>

</androidx.constraintlayout.widget.ConstraintLayout>

效果如下

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

在阿里云中查看已保存的數(shù)據(jù)

輸入:SELECT?*?FROM??'表名'?即可

Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)

資源在這里,里面包含代碼及mysql包:

https://download.csdn.net/download/shanhe_yuchuan/87610575

如有問(wèn)題歡迎評(píng)論區(qū)或私信,有時(shí)間我一定第一時(shí)間回復(fù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-448347.html

到了這里,關(guān)于Android+阿里云數(shù)據(jù)庫(kù),實(shí)現(xiàn)安卓云數(shù)據(jù)庫(kù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包