欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android連接MySQL數(shù)據(jù)庫實現(xiàn)方法詳解

 更新時間:2024年02月11日 12:16:38   作者:在下嗷嗚  
這篇文章主要介紹了Android連接MySQL數(shù)據(jù)庫實現(xiàn)方法,在Android應用程序中連接MySQL數(shù)據(jù)庫可以幫助開發(fā)人員實現(xiàn)更豐富的數(shù)據(jù)管理功能,而且在Android中操作數(shù)據(jù)庫真的太智能了,需要的朋友可以參考下

前言

  • 要為MySQL添加 非root用戶 并設置權限。一定要設置權限!!!默認是沒有權限的!!!

請注意為用戶設置主機時,主機設置為%時表示通配符,即任何主機均可使用本用戶連接,但不能使用localhost(但可以使用本機ipv4地址連接),想使用localhost連接需將用戶主機設置為localhost。

Android連MySQL因為不確定連接地址,所以用戶主機要設置為%

  • 在Android中連接MySQL的目標ip不能用//localhost//127.0.0.1,應使用真實的ip地址

(可用cmd查詢本機ip,cmd->ipconfig)

  • Android連接的MySQL版本應為5.X版本(8.X版本無法使用)驅動程序通用
  • 請注意?。。ndroid中連接/對數(shù)據(jù)庫操作 只能在子線程進行!!!!(單開一個線程)

因為是耗時操作!!!

如果url、賬號、密碼正確還報錯大概率是沒有在子線程操作數(shù)據(jù)庫。

  • 應為程序添加網(wǎng)絡及WIFI權限(通過網(wǎng)絡連接數(shù)據(jù)庫)
//必須加
<uses-permission android:name="android.permission.INTERNET"/>
//可不加
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  • 添加MySQL驅動程序

將JAR程序放入Project視圖模式下 app/libs中,并右鍵->Add As Library

  • 在url后加入如下語句能避免許多麻煩

"?useUnicode=true&characterEncoding=utf-8&useSSL=false"

  • 打開電腦的telnet服務,使其他人可以訪問
  • 打開電腦防火墻的MySQL端口,使其他人可以訪問
  • 建議以如下格式寫連接代碼

(1)ConnectionUtils 連接工具類,用于連接數(shù)據(jù)庫

(2)User 存儲內容類,用于存儲MySQL回傳數(shù)據(jù)

(3)UserDao 數(shù)據(jù)訪問對象,執(zhí)行SQL操作返回存儲內容類

  • 連接關閉后ResultSet中內容會消失(將其內容在連接關閉前存在其他地方)
  • 讀ResultSet內容前先移動指針 .next();如果ResultSet中無搜索結果,.next()方法返回false,有搜索結果返回true。
  • 設置連接MySQL超時 DriverManager.setLoginTimeout(2000);
  • 盡量復用通道,避免反復連接造成延遲!!!
  • ip地址問題

個人電腦一般在局域網(wǎng)內,其ip為NAT局域網(wǎng)ip,只能被同局域網(wǎng)設備訪問,無法直接被其他網(wǎng)絡訪問,可使用內網(wǎng)穿透軟件(如花生殼)進行處理。

  • MySQL其實就是服務器,可以直接進行遠程連接,不用再做服務器
  • MySQL自帶的4個數(shù)據(jù)庫不能刪!?。?/li>

information_schema、mysql、performation_schema、test

  • 可以多人同時使用同個用戶名及密碼登錄,但不建議
  • MySQL中建議使用PreparedStatement而不是使用Statement,PreparedStatement的執(zhí)行速度比Statement快并且可以防止SQL注入攻擊。在使用PreparedStatement時可以用?代替值,然后使用.setString(int index , String value)方法設置?的值,但請注意,PreparedStatement的index起始是1而不是0。要復用PreparedStatement!!

實例

  • ConnectionUtils 連接工具類,用于連接數(shù)據(jù)庫
  • User 存儲內容類,用于存儲MySQL回傳數(shù)據(jù)
  • UserDao 數(shù)據(jù)訪問對象,執(zhí)行SQL操作返回存儲內容類
//主線程  以監(jiān)聽為例
Thread thread=null;
public void onClick(View view){
      if(thread==null){
          //連接數(shù)據(jù)庫不能在主線程,單開一個線程
          thread=new Thread(new Runnable(){
              User user=UserDao.findUser(1);
          });
      }
}
//連接工具類
public class ConnectionUtils {
    public static Connection getConn(){
        String url="jdbc:mysql://255.255.255.1:3306/databaseName"+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username="firstUser";
        String password="123456";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Connection connection_jdbc = null;
        try {
            DriverManager.setLoginTimer(2000);
            connection_jdbc= DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection_jdbc;
    }
    public static boolean close(Connection connection){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
//存儲內容類
public class User {
    private int id;
    private String username;
    private String password;
    private int storyid;
    public int getId() {
        return id;
    }
    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
    public int getStoryid() {
        return storyid;
    }
    public User(int id,String username,String password,int storyid){
        this.id=id;
        this.username=username;
        this.password=password;
        this.storyid=storyid;
    }
    public String toString(){
        String str="id:"+id+" username:"+username+" password:"+password+" storyid:"+storyid;
        return str;
    }
}
//數(shù)據(jù)訪問對象類
public class Dao {
    private Connection conn;
    public static User findUser(int id){
        //盡量復用通道,避免反復連接造成延遲
        if(conn==null){
            conn=ConnectionUtils.getConn();
        }
        try {
            Statement statement=conn.createStatement();
            String sql="select * from mysql where id ='"+id+"'";
            ResultSet resultSet=statement.executeQuery(sql);
            Boolean bool = resultSet.next();//先移動指針再獲取值
            //如果ResultSet無結果,next()返回false
            if(bool){
               int resultSetId=resultSet.getInt("id");
               String resultSetSex=resultSet.getString("sex");
               String resultSetName=resultSet.getString("name");
               User user=new User(resultSetId,resultSetSex,resultSetName);
               return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //確定不使用再關閉通道
        //ConnectionUtils.close(conn);
    }
}

常見報錯原因

  • 程序 對/連接 數(shù)據(jù)庫操作 是否在子線程
  • mysql用戶主機是否設置%(任意主機)或其他
  • mysql用戶權限是否設置
  • 程序連接時的用戶名與密碼是否正確

連接緩慢原因

  • 程序是否復用通道connection,反復連接數(shù)據(jù)庫會導致緩慢
  • prepareStatement代替statement,減少命令的創(chuàng)建(要復用PreparedStatement)

tag: java ,遠程連接 ,mysql ,Android ,安卓,MySQL

到此這篇關于Android連接MySQL數(shù)據(jù)庫實現(xiàn)方法詳解的文章就介紹到這了,更多相關Android連接MySQL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論